﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug
{
    public partial class RM69330 : Form
    {
        private bool Listening = false;
        private bool IClosing = false;
        #region 
        CaSDK Ca310Obj = new CaSDK();
        private double auto_lum;
        private double auto_x;
        private double auto_y;
        private double auto_T;
        private double auto_duv;

        private void Msr_CA310()
        {
            try
            {
                if (rbt_ca410.Checked == true)
                {
                    CaSDKObj.CA410_Measure_Lxy();
                    auto_lum = CaSDKObj.Lv;
                    auto_x = CaSDKObj.sx;
                    auto_y = CaSDKObj.sy;
                }
                else //(rbt_ca310.Checked == true)
                {
                    MainForm.Msr_CASDK();
                    auto_lum = MainForm.CASDK_LUM;
                    auto_x = MainForm.CASDK_x;
                    auto_y = MainForm.CASDK_y;
                }
                CA310_x.Text = "" + auto_x;
                CA310_y.Text = "" + auto_y;
                CA310_Lum.Text = "" + auto_lum;
            }
            catch
            {
                textBox1.Text += ("CA310/410未校准或者镜头被遮挡或读数异常！\r\n");
                //textBox1.Focus();//获取焦点
                textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
                textBox1.ScrollToCaret();//滚动到光标处
                return;
            }
        }
        CaSDK CaSDKObj = new CaSDK();
        private void btnCal_Click(object sender, EventArgs e)   //CA310连接校准
        {
            try
            {
                Tim1Ca310.Start();//显示提示310连接提示信息
                textBox1.Text += "CA310/410 连接校准中...\r\n";
                OLED_Show_Click(btnblack, e);
                Application.DoEvents();//重点，必须加上，否则提示信息不能按顺序显示出来
            }
            catch
            {
                Thread.Sleep(100);
            }
            try
            {
                if (rbt_ca410.Checked == true)
                {
                    CaSDKObj.AutoConnect();
                    CaSDKObj.SingleCalibration(0, byte.Parse(CA310_Channel.Text), double.Parse(CA410_Measure_Freq.Text));
                }
                else
                {
                    CaSDKObj.CA310_Connect();
                    CaSDKObj.CA310_Initial(byte.Parse(CA310_Channel.Text));
                }
                //btnCal_manual.Text = "Wait"; Application.DoEvents();
                btn_CAS200_Start.Enabled = true;
                CAS200_dataGridView.Enabled = true;
                btn_CAS200_Save.Enabled = true;
                btn_CAS200_MeasureOnce.Enabled = true;
                btn_CAS200_Clean.Enabled = true;
                btn_CAS200_Delect.Enabled = true;
                gBx_CAS200_Auto_Test.Enabled = true;
                btn_CAS200_Stop.Enabled = true;
                //btnCal_manual.Text = "0Cal";
                btnAutoGamma.Enabled = true;
            }
            catch (Exception EX)
            {
                MessageBox.Show("CA310/410 Connect Fail!!" + EX.Message);
            }

            btnCal_manual.BackColor = Color.Gray;
            btnAutoGamma.Enabled = true;
            textBox1.Text += "CA310/410 连接完成...\r\n";
            //btnSetRGBBase.Enabled = true;

        }
        private void Tim1Ca310_Tick(object sender, EventArgs e)
        {


            Tim1Ca310.Enabled = false;
        }
        private void btn_DisconnectCaX10_Click(object sender, EventArgs e)
        {
            //Ca310Obj.objCa.RemoteMode = 0;
        }
        #endregion

        #region 
        CaSDK Ca41Obj = new CaSDK();
        public double Lv = 0.0;
        public double sx = 0.0;
        public double sy = 0.0;

        #endregion

        private List<byte> buffer = new List<byte>(2048);
        private byte[] Rec_tmp = new byte[1];
        private byte[] Rec_tmp1 = new byte[1];
        private byte[] Send_buf = new byte[1];
        private byte signal_mode;
        public byte start_trans_flag = 0;
        public byte continueflag = 0;


        public byte RM350_0XFE_XX = 0;

        #region

        frmMain MainForm = null;
        public RM69330(frmMain Frm)
        {
            MainForm = Frm;
            InitializeComponent();
            // MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
        }
        private void btnBack_Click(object sender, EventArgs e)
        {
            MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
            MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);
            MainForm.Show();
            this.Hide();
        }

        private void btnCa410Connect_Click(object sender, EventArgs e)
        {
            //string ca410Info = "";
            //Ca410.AutoConnect();
            //Ca410.DefaultSetting();

            //Ca410.Measurement(ref Lv, ref sx, ref sy, ref ca410Info);
            //textBox1.Text += "Lv " + Lv + "\r\n";
            //textBox1.Text += ca410Info + "\r\n";
        }




        private void btnCal_manual_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.btnCal_manual, "CA310连接校准，自带黑画面，按下静候6秒");
        }

        public class MyImage
        {
            public MyImage(string p)
            {
                this.Path = p;
            }
            public string Path { get; set; }
            public override string ToString()
            {
                if (Path == null)
                {
                    return null;
                }
                return System.IO.Path.GetFileName(this.Path);
            }
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            string path = fldb.SelectedPath;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            DirectoryInfo di = new DirectoryInfo(path);
            this.listBox3_BMPFile.Items.Clear();
            this.CreateListItem(di);
            textBox2.Text = path;
        }

        private void CreateListItem(DirectoryInfo di)
        {
            foreach (DirectoryInfo childDi in di.GetDirectories())
            {
                this.CreateListItem(childDi);
            }
            foreach (FileInfo fi in di.GetFiles())
            {
                if (ImageType.Contains(fi.Extension))
                {
                    this.listBox3_BMPFile.Items.Add(new MyImage(fi.FullName));
                }
            }
        }
        private void lstImage_Click(object sender, EventArgs e)
        {
            if (listBox3_BMPFile.SelectedItem == null)
            {
                return;
            }
            MyImage selectedImg = (MyImage)listBox3_BMPFile.SelectedItem;
            Image img = new Bitmap(selectedImg.Path);
            pictureBox1.Image = img;
        }
        System.DateTime TimeNow2 = new DateTime();
        TimeSpan TimeCount2 = new TimeSpan();
        private void btnPrevPic_Click(object sender, EventArgs e)
        {
            FileStream aFile;
            Int32 m_txlen = 0;

            int HACT_True;
            int VACT_True;
            int j;
            TimeNow2 = DateTime.Now; //开始计时

            byte[] TemFileContent = new byte[512 * 4000 + 100];
            int ByteReaded = 0x00;

            string FileName = textBox2.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString();

            aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            ByteReaded = aFile.Read(TemFileContent, 0, 54);

            HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
            VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);
            if (MainForm.HACT.Value != HACT_True)
            {
                textBox1.Text += "图片宽度与设定不符\r\n";
                return;
            }

            if ((MainForm.Dis_mode.SelectedIndex == 1)) //(Dis_mode.SelectedIndex == 1):MIPI 模式，因为下位机仅对MIPI模式做了优化
            {
                //------------20180903---------------
                try
                {   //bd 5a a5 9DF0-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    //BD A5 5A 9DF0-----提前下个标志位告诉下位机开始 快速 送图了
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0xa5; TemFileContent[2] = 0x5a; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    MainForm.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    j = 0;
                    int x, y, s = 0;
                    x = 512 * 4000; //单次传输数量
                    y = (VACT_True * HACT_True * 3 / x); //传输次数
                    if ((VACT_True * HACT_True * 3 % x) != 0)
                        y++;
                    {
                        //aFile.Seek(54, SeekOrigin.Begin);
                        while (j < y)
                        {
                            //if (HACT_True % 4 != 0)
                            //    aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            //else
                            //    aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                            aFile.Seek((j) * x + 54, SeekOrigin.Begin);

                            ByteReaded = aFile.Read(TemFileContent, 0, x);

                            try
                            {
                                MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }

                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            MainForm.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }

                        aFile.Close();
                    }
                }
                catch
                {
                    MessageBox.Show("请选择文件夹", "错误");
                }
            }
            else
            {
                //------------20180808---------------
                try
                { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    MainForm.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    j = 1;
                    //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                    {
                        aFile.Seek(54, SeekOrigin.Begin);
                        while (j <= VACT_True)
                        {
                            if (HACT_True % 4 != 0)
                                aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            else
                                aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);


                            ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 

                            try
                            {
                                MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }
                            m_txlen += ByteReaded;
                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            MainForm.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            return;
                        }

                        aFile.Close();
                    }
                    /*
                    else
                    {
                        MessageBox.Show("BMP分辨率非法");
                    }*/
                }
                catch
                {
                    textBox1.Text += "啥图片都没显示";
                    textBox1.Select(textBox1.Text.Length, 0);
                    textBox1.ScrollToCaret();
                }
            }


            TimeCount2 = DateTime.Now - TimeNow2;
            textBox1.Text = string.Format("{0:00}:{1:00}", TimeCount2.Seconds, TimeCount2.Milliseconds);
        }
        private void RM69330_Load(object sender, EventArgs e)
        {
            string[] s = { ".bmp" };
            this.ImageType = new List<string>(s);
        }

        private void btnCleantextbox1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

        private void Frm_Activated(object sender, EventArgs e)
        {

        }
        private void RM692Ax_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            btnBack_Click(btnBack, e);
        }
        #endregion
        public byte Seil_port_Find_flag = 0;
        private byte[] Driver_IC = new byte[1];
        private List<string> ImageType;
        private delegate void MonitorSerialPortChangeEventHandler();
        private MonitorSerialPortChangeEventHandler MonitorCom;
        private int serialPortStatus;

        public byte RM2AX_0XFE_XX = 0;

        CRC16 CRCData = new CRC16();

        #region
        private void T1_Tick(object sender, EventArgs e)
        {
            T1.Enabled = false;
            textBox1.Text = textBox1.Text + "通讯故障!\r\n";
            textBox1.Focus();
            textBox1.Select(textBox1.TextLength, 0);
            textBox1.ScrollToCaret();
        }

        #endregion
        #region  
        private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            try
            {
                Listening = true;
                int n = MainForm.comPort.BytesToRead;
                byte[] buf = new byte[n];


                MainForm.comPort.Read(buf, 0, n);
                bool data_1_catched = false;
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    if (buffer[0] == 0x0B)
                    {
                        if (buffer.Count >= (Int16)(buffer[2] << 8) + (buffer[3]) + 7)
                        {
                            Rec_tmp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            byte[] crcTemp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            buffer.CopyTo(0, crcTemp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 2] != CRCH && buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 1] != CRCL)
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);
                                data_1_catched = true;
                                buffer.RemoveRange(0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);
                            }

                        }
                    }
                    else if (buffer[0] == 0xFE)
                    {
                        Rec_tmp = new byte[buffer[2] + 5];
                        byte[] crcTemp = new byte[buffer[2] + 5];
                        buffer.CopyTo(0, crcTemp, 0, buffer[2] + 5);

                        CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                        if (buffer[buffer[2] + 5 - 2] != CRCH && buffer[buffer[2] + 5 - 1] != CRCL)
                        {
                            buffer.RemoveRange(0, buffer.Count);
                        }
                        else
                        {
                            buffer.CopyTo(0, Rec_tmp, 0, buffer[2] + 5);
                            data_1_catched = true;
                            buffer.RemoveRange(0, buffer[2] + 5);
                            if (Rec_tmp[4] == 0)
                                continueflag = 1;
                        }
                    }
                    else if (buffer[0] == 0x55)
                    {
                        if (buffer.Count >= (buffer[2] + 5))
                        {
                            Rec_tmp = new byte[(buffer[2] + 5)];
                            byte[] crcTemp = new byte[(buffer[2] + 5)];
                            buffer.CopyTo(0, crcTemp, 0, (buffer[2] + 5));

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(buffer[2] + 5) - 2] != CRCH && buffer[(buffer[2] + 5) - 1] != CRCL)
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (buffer[2] + 5));
                                data_1_catched = true;
                                buffer.RemoveRange(0, (buffer[2] + 5));
                                switch (Rec_tmp[3])
                                {
                                    case 0x00:
                                        {
                                            start_trans_flag = 1;
                                        }
                                        break;
                                    case 0x01:
                                        {
                                            if (MainForm.comPort.IsOpen == true) MainForm.comPort.Close();
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            start_trans_flag = 0;
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }

                        }
                    }

                    else if ((buffer[0] == 0x32) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);
                            }
                        }
                    }
                    else
                        buffer.RemoveRange(0, buffer.Count);
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        T1.Enabled = false;


                        if ((Rec_tmp[0] == 0x09) || (Rec_tmp[0] == 0x19) || (Rec_tmp[0] == 0x29) || (Rec_tmp[0] == 0x2e))
                        {

                        }

                        else if (Rec_tmp[0] == 0x32)
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01: { if (Rec_tmp[4] == 0x00) textBox1.Text = textBox1.Text + "写寄存器\tOK！\r\n"; } break;
                                case 0x02:
                                    {
                                        if (Setting.TempRecord.RM69330CmdType == 0x01)
                                        {
                                            string tmp = "";
                                            tmp = String.Format("{0:X2}", Rec_tmp[4]);
                                            RM69330_Reg_Data.Text = tmp;
                                            textBox1.Text = textBox1.Text + "读寄存器\tOK！\r\n";
                                        }
                                    }
                                    break;

                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            if ((Rec_tmp[3] & 0xf0) == 0x00) textBox1.Text = textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x40) textBox1.Text = textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x80) textBox1.Text = textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Gamma1 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Gamma1 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Gamma2 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Gamma2 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0E:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Gamma3 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Gamma3 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0F:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写GOA OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写GOA OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x10:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Power OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Power OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0B:
                                    {
                                        string tmp = "";
                                        tmp = String.Format("{0:X2}", Rec_tmp[4]);
                                        textBox9.Text = tmp;
                                        textBox1.Text = textBox1.Text + "OTP\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x0C:
                                    {
                                        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Others OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Others OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x11:
                                    {
                                        switch (Rec_tmp[3])
                                        {
                                            case 0x01:
                                            case 0x02:
                                            case 0x03:
                                                {
                                                    textBox1.Text = textBox1.Text + "Gamma切换成功！\r\n";
                                                    if (radio_RM330_27BD.Checked == true)
                                                    {
                                                        RM330_R19.Enabled = true; RM330_G19.Enabled = true; RM330_B19.Enabled = true;
                                                        RM330_R23.Enabled = true; RM330_G23.Enabled = true; RM330_B23.Enabled = true;
                                                        RM330_R27.Enabled = true; RM330_G27.Enabled = true; RM330_B27.Enabled = true;
                                                        RM330_R35.Enabled = true; RM330_G35.Enabled = true; RM330_B35.Enabled = true;
                                                        RM330_R39.Enabled = true; RM330_G39.Enabled = true; RM330_B39.Enabled = true;
                                                        RM330_R47.Enabled = true; RM330_G47.Enabled = true; RM330_B47.Enabled = true;
                                                        RM330_R55.Enabled = true; RM330_G55.Enabled = true; RM330_B55.Enabled = true;
                                                        RM330_R79.Enabled = true; RM330_G79.Enabled = true; RM330_B79.Enabled = true;
                                                        RM330_R111.Enabled = true; RM330_G111.Enabled = true; RM330_B111.Enabled = true;
                                                        RM330_R143.Enabled = true; RM330_G143.Enabled = true; RM330_B143.Enabled = true;
                                                        RM330_R175.Enabled = true; RM330_G175.Enabled = true; RM330_B175.Enabled = true;
                                                        RM330_R207.Enabled = true; RM330_G207.Enabled = true; RM330_B207.Enabled = true;
                                                        RM330_R223.Enabled = true; RM330_G223.Enabled = true; RM330_B223.Enabled = true;
                                                        RM330_R239.Enabled = true; RM330_G239.Enabled = true; RM330_B239.Enabled = true;
                                                    }
                                                    else if (radio_RM330_13BD.Checked == true)
                                                    {
                                                        RM330_R19.Enabled = false; RM330_G19.Enabled = false; RM330_B19.Enabled = false;
                                                        RM330_R23.Enabled = false; RM330_G23.Enabled = false; RM330_B23.Enabled = false;
                                                        RM330_R27.Enabled = false; RM330_G27.Enabled = false; RM330_B27.Enabled = false;
                                                        RM330_R35.Enabled = false; RM330_G35.Enabled = false; RM330_B35.Enabled = false;
                                                        RM330_R39.Enabled = false; RM330_G39.Enabled = false; RM330_B39.Enabled = false;
                                                        RM330_R47.Enabled = false; RM330_G47.Enabled = false; RM330_B47.Enabled = false;
                                                        RM330_R55.Enabled = false; RM330_G55.Enabled = false; RM330_B55.Enabled = false;
                                                        RM330_R79.Enabled = false; RM330_G79.Enabled = false; RM330_B79.Enabled = false;
                                                        RM330_R111.Enabled = false; RM330_G111.Enabled = false; RM330_B111.Enabled = false;
                                                        RM330_R143.Enabled = false; RM330_G143.Enabled = false; RM330_B143.Enabled = false;
                                                        RM330_R175.Enabled = false; RM330_G175.Enabled = false; RM330_B175.Enabled = false;
                                                        RM330_R207.Enabled = false; RM330_G207.Enabled = false; RM330_B207.Enabled = false;
                                                        RM330_R223.Enabled = false; RM330_G223.Enabled = false; RM330_B223.Enabled = false;
                                                        RM330_R239.Enabled = false; RM330_G239.Enabled = false; RM330_B239.Enabled = false;
                                                    }
                                                }
                                                break;
                                            default: break;
                                        }

                                    }
                                    break;
                                case 0x09:
                                    {
                                        if (Setting.TempRecord.RM69330CmdType == 0x03)
                                        {
                                            if (radio_RM330_27BD.Checked == true)
                                            {
                                                RM330_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                                RM330_R1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                RM330_R3.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                RM330_R7.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                RM330_R11.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                RM330_R15.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                RM330_R19.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                RM330_R23.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                RM330_R27.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                RM330_R31.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                RM330_R35.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                RM330_R39.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                RM330_R47.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                RM330_R55.Value = Decimal.Parse((Rec_tmp[30] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                RM330_R63.Value = Decimal.Parse((Rec_tmp[32] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                RM330_R79.Value = Decimal.Parse((Rec_tmp[34] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                RM330_R95.Value = Decimal.Parse((Rec_tmp[36] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                RM330_R111.Value = Decimal.Parse((Rec_tmp[38] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                RM330_R127.Value = Decimal.Parse((Rec_tmp[40] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                RM330_R143.Value = Decimal.Parse((Rec_tmp[42] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                RM330_R159.Value = Decimal.Parse((Rec_tmp[44] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                RM330_R175.Value = Decimal.Parse((Rec_tmp[46] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                RM330_R191.Value = Decimal.Parse((Rec_tmp[48] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                RM330_R207.Value = Decimal.Parse((Rec_tmp[50] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                RM330_R223.Value = Decimal.Parse((Rec_tmp[52] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                RM330_R239.Value = Decimal.Parse((Rec_tmp[54] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                RM330_R255.Value = Decimal.Parse((Rec_tmp[56] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            }
                                            else if (radio_RM330_13BD.Checked == true)
                                            {
                                                RM330_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                                RM330_R1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                RM330_R3.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                RM330_R7.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                RM330_R11.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                RM330_R15.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                RM330_R31.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                RM330_R63.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                RM330_R95.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                RM330_R127.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                RM330_R159.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                RM330_R191.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                RM330_R255.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            }
                                            textBox1.Text = textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.RM69330CmdType == 0x04)
                                        {
                                            if (radio_RM330_27BD.Checked == true)
                                            {
                                                RM330_G0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                                RM330_G1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                RM330_G3.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                RM330_G7.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                RM330_G11.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                RM330_G15.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                RM330_G19.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                RM330_G23.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                RM330_G27.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                RM330_G31.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                RM330_G35.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                RM330_G39.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                RM330_G47.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                RM330_G55.Value = Decimal.Parse((Rec_tmp[30] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                RM330_G63.Value = Decimal.Parse((Rec_tmp[32] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                RM330_G79.Value = Decimal.Parse((Rec_tmp[34] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                RM330_G95.Value = Decimal.Parse((Rec_tmp[36] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                RM330_G111.Value = Decimal.Parse((Rec_tmp[38] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                RM330_G127.Value = Decimal.Parse((Rec_tmp[40] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                RM330_G143.Value = Decimal.Parse((Rec_tmp[42] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                RM330_G159.Value = Decimal.Parse((Rec_tmp[44] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                RM330_G175.Value = Decimal.Parse((Rec_tmp[46] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                RM330_G191.Value = Decimal.Parse((Rec_tmp[48] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                RM330_G207.Value = Decimal.Parse((Rec_tmp[50] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                RM330_G223.Value = Decimal.Parse((Rec_tmp[52] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                RM330_G239.Value = Decimal.Parse((Rec_tmp[54] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                RM330_G255.Value = Decimal.Parse((Rec_tmp[56] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            }
                                            else if (radio_RM330_13BD.Checked == true)
                                            {
                                                RM330_G0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                                RM330_G1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                RM330_G3.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                RM330_G7.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                RM330_G11.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                RM330_G15.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                RM330_G31.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                RM330_G63.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                RM330_G95.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                RM330_G127.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                RM330_G159.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                RM330_G191.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                RM330_G255.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            }
                                            textBox1.Text = textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.RM69330CmdType == 0x05)
                                        {
                                            if (radio_RM330_27BD.Checked == true)
                                            {
                                                RM330_B0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                                RM330_B1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                RM330_B3.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                RM330_B7.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                RM330_B11.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                RM330_B15.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                RM330_B19.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                RM330_B23.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                RM330_B27.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                RM330_B31.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                RM330_B35.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                RM330_B39.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                RM330_B47.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                RM330_B55.Value = Decimal.Parse((Rec_tmp[30] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                RM330_B63.Value = Decimal.Parse((Rec_tmp[32] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                RM330_B79.Value = Decimal.Parse((Rec_tmp[34] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                RM330_B95.Value = Decimal.Parse((Rec_tmp[36] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                RM330_B111.Value = Decimal.Parse((Rec_tmp[38] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                RM330_B127.Value = Decimal.Parse((Rec_tmp[40] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                RM330_B143.Value = Decimal.Parse((Rec_tmp[42] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                RM330_B159.Value = Decimal.Parse((Rec_tmp[44] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                RM330_B175.Value = Decimal.Parse((Rec_tmp[46] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                RM330_B191.Value = Decimal.Parse((Rec_tmp[48] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                RM330_B207.Value = Decimal.Parse((Rec_tmp[50] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                RM330_B223.Value = Decimal.Parse((Rec_tmp[52] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                RM330_B239.Value = Decimal.Parse((Rec_tmp[54] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                RM330_B255.Value = Decimal.Parse((Rec_tmp[56] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            }

                                            else if (radio_RM330_13BD.Checked == true)
                                            {
                                                RM330_B0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                                RM330_B1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                RM330_B3.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                RM330_B7.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                RM330_B11.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                RM330_B15.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                RM330_B31.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                RM330_B63.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                RM330_B95.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                RM330_B127.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                RM330_B159.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                RM330_B191.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                RM330_B255.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            }
                                            textBox1.Text = textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }

                                    }
                                    break;
                                default: break;

                            }
                        }


                        else if (Rec_tmp[0] == 0x2F)
                        {
                            if ((Rec_tmp[7] == 0x2E) || (Rec_tmp[7] == 0x3C))
                            {
                                if (Rec_tmp[7] == 0x2E)
                                {
                                    textBox1.Text = textBox1.Text + "297 298 299\r\n";
                                }
                                else if (Rec_tmp[7] == 0x3C)
                                {
                                    textBox1.Text = textBox1.Text + "350\r\n";
                                }
                                switch (Rec_tmp[1])
                                {
                                    case 0x01:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura RAM Enable\r\n";
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura RAM Disable\r\n";
                                        }
                                        break;
                                    case 0x03:
                                        {
                                            if (Rec_tmp[7] == 0x2E)
                                            {
                                                if (Rec_tmp[4] == 0x80)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash complete ! \r\n";
                                                }
                                            }
                                            else if (Rec_tmp[7] == 0x3C)
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash1 complete ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x0A:
                                        {
                                            if (Rec_tmp[7] == 0x3C)
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash2 complete ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x04:
                                        {
                                            textBox1.Text = textBox1.Text + "Checksum: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                        }
                                        break;
                                    case 0x05:
                                        {
                                            if (Rec_tmp[7] == 0x2E)
                                            {
                                                if (Rec_tmp[4] == 0x80)
                                                {
                                                    textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                                }
                                            }
                                            else if (Rec_tmp[7] == 0x3C)
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x06:
                                        {
                                            textBox1.Text = textBox1.Text + "flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "\r\n";
                                        }
                                        break;
                                    case 0x07:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura function on \r\n";
                                        }
                                        break;
                                    case 0x08:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura function off \r\n";
                                        }
                                        break;
                                    case 0x09:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura OTP complete$ \r\n";
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }

                        textBox1.Focus();
                        textBox1.Select(textBox1.TextLength, 0);
                        textBox1.ScrollToCaret();
                        if (textBox1.Lines.Length > 200) textBox1.Text = "";
                    }));
                }
            }
            finally
            {
                Listening = false;

            }

        }
        #endregion

        #region
        private void Pattern_Write(byte R_value, byte G_value, byte B_value)
        {
            numPatternR.Value = R_value;
            numPatternG.Value = G_value;
            numPatternB.Value = B_value;
            OLED_Show_Click(btnPatternW, null);
        }
        private void OLED_Show_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            Send_buf[0] = 0x0A;
            Send_buf[2] = 0x15;
            switch (btnTmp.Name)
            {

                case "btnPatternW":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                        Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                        Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                    }
                    break;





                case "btnred":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break;
                case "btngreen":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0;
                    }
                    break;
                case "btnblue":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0xFF;
                    }
                    break;
                case "btnwhite":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0xFF;
                    }
                    break;
                case "btnblack":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break;
                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 25);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            Thread.Sleep(800);
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
                numPatternG.Enabled = false;
                numPatternB.Enabled = false;
            }
            else
            {
                numPatternG.Enabled = true;
                numPatternB.Enabled = true;
            }
        }
        private void numPatternR_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
            }
        }

        private void numPatternG_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternB.Value = numPatternG.Value;
                numPatternR.Value = numPatternB.Value;
            }
        }

        private void numPatternB_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternB.Value;
                numPatternR.Value = numPatternG.Value;
            }
        }
        private void CAS200_Measure_Information(double Dgv_lum, double Dgv_x, double Dgv_y)
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;

            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.CAS200_dataGridView.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = false;
                        this.CAS200_dataGridView.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.CAS200_dataGridView.Rows.Add(Row1);
            }
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;

            this.CAS200_dataGridView.Rows[Row_Accum].Cells[1].Value = Dgv_x.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[2].Value = Dgv_y.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[3].Value = Dgv_lum.ToString("#0.0000");
            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
            }
            CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
        }
        private double R_x = 0.67;
        private double R_y = 0.33;
        private double G_x = 0.22;
        private double G_y = 0.71;
        private double B_x = 0.14;
        private double B_y = 0.06;
        private double lum_off, x_off, y_off;
        #endregion

        #region 
        Gamma_Addr GAMMAs = new Gamma_Addr();
        private ushort[] gamma_r = new ushort[27];
        private ushort[] gamma_g = new ushort[27];
        private ushort[] gamma_b = new ushort[27];

        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;

        #region  
        System.DateTime TimeNow = new DateTime();
        TimeSpan TimeCount = new TimeSpan();
        private void TimTest_Tick(object sender, EventArgs e)
        {
            TimeCount = DateTime.Now - TimeNow;
            txtTimeShow.Text = string.Format("{0:00}:{1:00}:{2:00}", TimeCount.Hours, TimeCount.Minutes, TimeCount.Seconds);
        }
        #endregion
        private int adjust_Rnum = 0;
        private int adjust_Gnum = 0;
        private int adjust_Bnum = 0;
        private int adjust_l = 0;
        private int adjust_x = 0;
        private int adjust_y = 0;
        private int index = 0;
        private double[,] D_lums;
        private double[] D_lum;
        private double D_x, D_y;
        private bool[] PointCheck = new bool[31];
        private static bool GammaAdjustTimeoutFlag = false;
        private static int GammaAdjustTimes = 0;
        private void GammaUpSpeed(double targetlum, double nowlum, int nowpoint, int nowRegValue, int lastpoint, int lastRegValue, out int nowRegValueAdj)
        {
            int step_tmp;
            if (nowRegValue > lastRegValue)
            {
                nowRegValue = lastRegValue - 1;
            }
            if (Math.Abs(nowlum - targetlum) > targetlum * 20 / 100)
            {
                if (lastpoint - nowpoint > 6) { step_tmp = 6; }
                else step_tmp = 3;
            }
            else if (Math.Abs(nowlum - targetlum) > targetlum * 10 / 100)
            {
                if (lastpoint - nowpoint > 4) { step_tmp = 6; }
                else step_tmp = 3;
            }
            else
            {
                step_tmp = 1;
            }

            if (nowlum > 0)
            {
                if (nowlum > targetlum)
                    nowRegValueAdj = nowRegValue - step_tmp;
                else nowRegValueAdj = nowRegValue + step_tmp;
            }
            else nowRegValueAdj = lastRegValue - 10;

        }
        byte k = 0;

        bool lumdataReadyFlag;

        #endregion
        #region 
        private byte[] Point_RM69330 = new byte[27] { 0, 1, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 47, 55, 63, 79, 95, 111, 127, 143, 159, 175, 191, 207, 223, 239, 255 };
        private int CAS200_Measure_Gray = 0; //51值
        private int CAS200_Measure_Count = 0;
        private double[] lumRead = new double[31];
        private void T3_Tick(object sender, EventArgs e)
        {
            Int32 step;

            step = (Convert.ToUInt16(CAS200_MeasureStep.Text));

            if (cbx_follow_51_points.Checked == true)
            {

                {
                    if (RM330_0XFE_XX != 0xF0)
                    {
                        RM330_0XFE_XX = 0xF0;
                        RM69330_Reg_Addr.Text = "fe";
                        RM69330_Reg_Data.Text = "00";
                        RM330_Others_Click(RM69330_51Reg_W, e);
                    }

                    if (CAS200_Measure_Gray > 0)//51值
                    {
                        RM69330_51Reg_trackBar1.Value = CAS200_Measure_Gray - 1;
                    }
                    else
                    {
                        RM69330_51Reg_trackBar1.Value = 0;
                    }
                    RM692AX_51Set((UInt16)RM69330_51Reg_trackBar1.Value);

                    //RM69330_51Reg_trackBar1.Text = CAS200_Measure_Gray.ToString() + " 阶";
                    RM69330_51Reg.Text = (RM69330_51Reg_trackBar1.Value).ToString() + " 阶";
                }
                CAS200_Measure_Gray -= step;
                if (CAS200_Measure_Gray < 0)
                {
                    T3.Enabled = false;
                }
            }
            else if (cbx_follow_gamma_points.Checked == true)
            {
                T3.Enabled = false;

                if (CAS200_Measure_Count > 25)
                {
                    T3.Enabled = false;
                    textBox1.Text += "绑点数据量测完毕\r\n";
                    lumdataReadyFlag = true;
                    return;
                }

                numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_RM69330[CAS200_Measure_Count];
                Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
                T3.Enabled = true;
                CAS200_Measure_Count++;
            }
            else
            {
                if ((Convert.ToInt16(numPatternR.Value.ToString()) + Convert.ToInt16(numPatternB.Value.ToString()) + Convert.ToInt16(numPatternG.Value.ToString()) - 3 * int.Parse(CAS200_MeasureStep.Text)) >= 0)
                {
                    Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
                    if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
                    {
                        if (numPatternR.Value != 0)
                        {
                            numPatternR.Value = (byte)(Convert.ToInt16(numPatternR.Value.ToString()) - int.Parse(CAS200_MeasureStep.Text));
                        }
                        if (numPatternG.Value != 0)
                        {
                            numPatternG.Value = (byte)(Convert.ToInt16(numPatternG.Value.ToString()) - int.Parse(CAS200_MeasureStep.Text));
                        }
                        if (numPatternB.Value != 0)
                        {
                            numPatternB.Value = (byte)(Convert.ToInt16(numPatternB.Value.ToString()) - int.Parse(CAS200_MeasureStep.Text));
                        }
                    }
                    else
                    {
                        numPatternB.Value = numPatternG.Value = numPatternR.Value = (byte)(Convert.ToInt16(numPatternR.Value.ToString()) - int.Parse(CAS200_MeasureStep.Text));
                    }

                }
                else
                {
                    T3.Enabled = false;
                }
            }
            Thread.Sleep(Convert.ToInt32(CAS200_txt1.Text));
            btn_CAS200_Synt_Click(btn_CAS200_MeasureOnce, null);

            if ((cbx_follow_51_points.Checked == true) && (CAS200_Measure_Gray <= 0))
            {
                RM692AX_51Set((UInt16)(RM69330_51Reg_trackBar1.Maximum / 2));
            }
        }
        private void btn_CAS200_Synt_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            switch (btnTmp.Name)
            {
                case "btn_CAS200_MeasureOnce":
                    {
                        Msr_CA310();
                        CAS200_Measure_Information(auto_lum, auto_x, auto_y);
                    }
                    break;
                case "btn_CAS200_Save":
                    {
                        SaveFileDialog sf = new SaveFileDialog();
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*";

                        string str = "序号,x坐标,y坐标,亮度\r\n";
                        if (sf.ShowDialog() == DialogResult.OK)
                        {
                            if (sf.FileName == "") return;
                            try
                            {
                                StreamWriter sw = null;
                                sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                                for (int i = 0; i < (this.CAS200_dataGridView.Rows.Count - 1); i++)
                                {
                                    str = str + this.CAS200_dataGridView.Rows[i].Cells[0].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[1].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[2].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[3].Value.ToString() + "\r\n";

                                }
                                sw.WriteLine(str);
                                sw.Dispose();
                                sw.Close();
                                MessageBox.Show("CA310数据保存OK！", "系统提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据保存时发生错误，请再试！", "系统提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_Load":
                    {
                        OpenFileDialog sf = new OpenFileDialog();
                        int Line_Count = 0;
                        string strtmp = "";
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*";
                        sf.Title = "数据导入";
                        if (sf.ShowDialog() == DialogResult.OK)
                        {
                            FileStream fs = new FileStream(sf.FileName, FileMode.Open, FileAccess.Read);
                            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                            string strLine = streamReader.ReadLine();
                            while (!string.IsNullOrEmpty(strLine))
                            {
                                strtmp = strtmp + strLine + ",";
                                strLine = streamReader.ReadLine();
                                Line_Count++;
                            }
                            fs.Close();
                            streamReader.Close();
                            string[] arrtmp = strtmp.Split(new Char[] { ',' });
                            for (int j = 1; j < Line_Count - 1; j++)
                            {
                                DataGridViewRow Row1 = new DataGridViewRow();
                                CAS200_dataGridView.Rows.Add(Row1);
                            }
                            try
                            {
                                for (int i = 0; i < Line_Count - 1; i++)
                                {
                                    CAS200_dataGridView.Rows[i].Cells[0].Value = arrtmp[4 * (i + 1) + 0];
                                    CAS200_dataGridView.Rows[i].Cells[1].Value = arrtmp[4 * (i + 1) + 1];
                                    CAS200_dataGridView.Rows[i].Cells[2].Value = arrtmp[4 * (i + 1) + 2];
                                    CAS200_dataGridView.Rows[i].Cells[3].Value = arrtmp[4 * (i + 1) + 3];
                                }
                                MessageBox.Show("CA310数据导入OK！", "提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据导入时发生错误！", "提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_Clean":
                    {
                        DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                        if (dr == DialogResult.OK)
                        {
                            this.CAS200_dataGridView.Rows.Clear();
                        }
                        else {; }
                    }
                    break;
                case "btn_CAS200_Delect":
                    {
                        int Current_Index, Row_Accum;
                        Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                        DataGridViewRow Row1 = CAS200_dataGridView.Rows[Current_Index];
                        this.CAS200_dataGridView.Rows.Remove(Row1);
                        this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                        Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                        for (Current_Index = 0; Current_Index < Row_Accum; Current_Index++)
                        {
                            this.CAS200_dataGridView.Rows[Current_Index].Cells[0].Value = Current_Index + 1;
                        }
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
                        CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
                    }
                    break;
                case "btn_CAS200_Stop":
                    {
                        T3.Enabled = false;
                    }
                    break;
                case "btn_CAS200_Start":
                    {
                        CAS200_Measure_Count = 0;//绑点计数

                        CAS200_Measure_Gray = 256;
                        numPatternR.Value = numPatternB.Value = numPatternG.Value = 255;
                        T3.Enabled = true;
                        T3.Interval = int.Parse(CAS200_txt1.Text);
                    }
                    break;
                default: break;
            }
        }
        private void CAS200_MeasureStep_TextChanged(object sender, EventArgs e)
        {
            UInt16 i = Convert.ToUInt16(CAS200_MeasureStep.Text);
            if ((i >= 1) && (i <= 8))
            {
                if ((i == 3) | (i == 5))
                {
                    i = 4;
                }
                else if ((i == 6) | (i == 7))
                {
                    i = 8;
                }
            }
            else
            {
                i = 1;
            }

            CAS200_MeasureStep.Text = i.ToString();
        }
        #endregion
        private void RM692AX_51Set(UInt16 valu51)
        {
            Send_buf = new byte[8];
            Setting.TempRecord.cmdLen = 8;
            valu51 &= 0x0FF;
            Send_buf[0] = 0x3E;
            Send_buf[1] = 0x51;
            Send_buf[2] = 0x03;
            Send_buf[3] = 0x51;
            Send_buf[4] = (Byte)(valu51);
            CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 8);
            }
            catch
            {
                MessageBox.Show("51set串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        private static bool Gamma_adjust_timeout_flag = false;
        private static int Gamma_auto_adjust_count = 0;
        //private void RM69330_Gamma_auto_Adujst(int lum_adjust_enable, int x_adjust_enable, int y_adjust_enable, int steph, int stepm, int stepl,
        //             double measure_lum, double measure_x, double measure_y, double target_lum, double target_x, double target_y, int adjust_times,
        //             decimal input_reg_red, decimal input_reg_green, decimal input_reg_blue,
        //             int gary_last_index2, int gary_last_index1, int gary_target,
        //             decimal index2_reg_red, decimal index2_reg_green, decimal index2_reg_blue,
        //             decimal index1_reg_red, decimal index1_reg_green, decimal index1_reg_blue,
        //             out decimal output_reg_red, out decimal output_reg_green, out decimal output_reg_blue, out bool timeout_flag)
        //{
        //    int step_tmp = 0;
        //    timeout_flag = false;
        //    output_reg_red = input_reg_red;
        //    output_reg_green = input_reg_green;
        //    output_reg_blue = input_reg_blue;
        //    try
        //    {
        //        if (Gamma_auto_adjust_count <= adjust_times)
        //        {
        //            Gamma_auto_adjust_count = Gamma_auto_adjust_count + 1;
        //            if (x_adjust_enable == 1)
        //            {
        //                if (Math.Abs(measure_x - target_x) > target_x * 10 / 100)
        //                {
        //                    step_tmp = steph;
        //                }
        //                else if (Math.Abs(measure_x - target_x) > target_x * 3 / 100)
        //                {
        //                    step_tmp = stepm;
        //                }
        //                else
        //                {
        //                    step_tmp = stepl;
        //                }
        //                if (measure_x > target_x)
        //                    output_reg_red = input_reg_red - step_tmp;
        //                else output_reg_red = input_reg_red + step_tmp;
        //            }
        //            if (lum_adjust_enable == 1)
        //            {
        //                if (Math.Abs(measure_lum - target_lum) > target_lum * 10 / 100)
        //                {
        //                    step_tmp = steph;
        //                }
        //                else if (Math.Abs(measure_lum - target_lum) > target_lum * 3 / 100)
        //                {
        //                    step_tmp = stepm;
        //                }
        //                else
        //                {
        //                    step_tmp = stepl;
        //                }
        //                if (measure_lum > target_lum)
        //                    output_reg_green = input_reg_green - step_tmp;
        //                else output_reg_green = input_reg_green + step_tmp;
        //            }
        //            if (y_adjust_enable == 1)
        //            {
        //                {
        //                    if (Math.Abs(measure_y - target_y) > target_y * 10 / 100)
        //                    {
        //                        step_tmp = steph;
        //                    }
        //                    else if (Math.Abs(measure_y - target_y) > target_y * 3 / 100)
        //                    {
        //                        step_tmp = stepm;
        //                    }
        //                    else
        //                    {
        //                        step_tmp = stepl;
        //                    }
        //                    if (measure_y > target_y)
        //                        output_reg_blue = input_reg_blue - step_tmp;
        //                    else output_reg_blue = input_reg_blue + step_tmp;
        //                }
        //            }
        //            if ((output_reg_red < 5) || (output_reg_red > index2_reg_red) || (output_reg_green < 5) || (output_reg_green > index2_reg_green) || (output_reg_blue < 5) || (output_reg_blue > index2_reg_blue))
        //            {
        //                output_reg_red = Convert.ToInt16(index1_reg_red - Math.Abs(index2_reg_red - index1_reg_red) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
        //                output_reg_green = Convert.ToInt16(index1_reg_green - Math.Abs(index2_reg_green - index1_reg_green) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
        //                output_reg_blue = Convert.ToInt16(index1_reg_blue - Math.Abs(index2_reg_blue - index1_reg_blue) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
        //                textBox1.Text = "数值超限，该绑点R/G/B插值迭代/r/n";
        //                Gamma_auto_adjust_count = 0;
        //                timeout_flag = true;
        //            }
        //        }
        //        else
        //        {
        //            textBox1.Text += "调整超时\r\n";
        //            Gamma_auto_adjust_count = 0;
        //            timeout_flag = true;
        //            if (output_reg_red > index1_reg_red)
        //            {
        //                output_reg_red = index1_reg_red - Math.Abs(index2_reg_red - index1_reg_red) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
        //                textBox1.Text += "该绑点R差值迭代/r/n";
        //            }
        //            if (output_reg_green > index1_reg_green)
        //            {
        //                output_reg_green = index1_reg_green - Math.Abs(index2_reg_green - index1_reg_green) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
        //                textBox1.Text += "该绑点G差值迭代/r/n";
        //            }
        //            if (output_reg_blue > index1_reg_blue)
        //            {
        //                output_reg_blue = index1_reg_blue - Math.Abs(index2_reg_blue - index1_reg_blue) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
        //                textBox1.Text += "该绑点B差值迭代/r/n";
        //            }
        //        }
        //        output_reg_red = Convert.ToInt32(output_reg_red);
        //        output_reg_green = Convert.ToInt32(output_reg_green);
        //        output_reg_blue = Convert.ToInt32(output_reg_blue);
        //    }
        //    catch
        //    {
        //        ;
        //    }
        //}

        private int[] Parttern_RM69330 = new int[26] { 255, 239, 223, 207, 191, 175, 159, 143, 127, 111, 95, 79, 63, 55, 47, 39, 35, 31, 27, 23, 19, 15, 11, 7, 3, 1 };
        private int[] Point_692AX = new int[26] { 255, 239, 223, 207, 191, 175, 159, 143, 127, 111, 95, 79, 63, 55, 47, 39, 35, 31, 27, 23, 19, 15, 11, 7, 3, 1 };
        //{ 0,1,  3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 47, 55, 63, 79, 95, 111, 127, 143, 159, 175, 191, 207, 223, 239, 255 };
        //private void T18_Tick(object sender, EventArgs e)
        //{
        //    double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;

        //    if (GammaAdjustTimes >= nudGammaAdjTimes.Value)
        //    {
        //        flag1 = flag2 = flag3 = 1; GammaAdjustTimes = 0;
        //        k++;
        //        if ((k > 5)&&(index>0)) 
        //        {
        //            k = 0;



        //            RGBValue[index, 0] = RGBValue[index-1, 0]-2;
        //            RGBValue[index, 1] = RGBValue[index-1, 0]-5;
        //            RGBValue[index, 2] = RGBValue[index-1, 0]-2;
        //            Value2Array((byte)index, array2Value);  
        //            auto_gamma_write(RM330_Gamma_r[26 - index], (UInt16)RGBValue[index, 0], RM330_Gamma_g[26 - index], (UInt16)RGBValue[index, 1], RM330_Gamma_b[26 - index], (UInt16)RGBValue[index, 2]);
        //        }

        //    }

        //    Msr_CA310();

        //    if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))
        //        flag1 = 0;
        //    if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)
        //        flag2 = 0;
        //    if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)
        //        flag3 = 0;















        //    if (flag1 == 0 && flag2 == 0 && flag3 == 0)
        //    {
        //        T18.Enabled = false;
        //        index++;
        //        adjust_Rnum = 0;
        //        GammaAdjustTimes = 0;

        //        adjust_Gnum = 0;
        //        adjust_Bnum = 0;
        //        adjust_l = 0;
        //        adjust_x = 0;

        //        adjust_y = 0;
        //        if (index == 24)
        //        {
        //            TimTest.Stop();
        //            textBox1.Text = textBox1.Text + "RM69330 gamma调试完毕!\r\n";
        //        }
        //        if (index < 24)
        //        {
        //            lum_off = Convert.ToDouble(txt_330_loff.Text);
        //            x_off = Convert.ToDouble(txt_330_xoff.Text);
        //            y_off = Convert.ToDouble(txt_330_yoff.Text);

        //            numPatternR.Value = Parttern_RM69330[index];
        //            numPatternG.Value = Parttern_RM69330[index];
        //            numPatternB.Value = Parttern_RM69330[index];
        //            OLED_Show_Click(btnPatternW, e);
        //            Thread.Sleep(10); 
        //            T18.Enabled = true; 
        //            flag1 = 1;
        //            flag2 = 1;
        //            flag3 = 1;

        //        }
        //    }
        //    else
        //    {
        //        decimal r, g, b;
        //        int flexible_gamma_count = 150;


        //        Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
        //        Lb_t = 1.0;
        //        Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
        //        lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
        //        lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
        //        lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

        //        Value2Array((byte)index, true);  

        //        {
        //            switch (index)
        //            {
        //                case 0:
        //                    {
        //                        if (flag3 == 1)
        //                        {   
        //                            GammaUpSpeed(D_lums[index, 0], lumr, Point_692AX[index], RGBValue[index, 0], 255, 0xfff, out int nowRegValueAdj);
        //                            adjust_l++;
        //                            if (adjust_l > 50)
        //                            {
        //                                x_off += 0.0005;  
        //                                adjust_l = 0;
        //                            }
        //                            if ((nowRegValueAdj > 0) && (nowRegValueAdj <= 0xfff))
        //                            {
        //                                RGBValue[index, 0] = nowRegValueAdj;    
        //                                Value2Array((byte)index, array2Value);  
        //                            }
        //                            else
        //                            {
        //                                T18.Enabled = false;
        //                                MessageBox.Show("R 自动调节失败，请手动重试");
        //                            }
        //                        }
        //                        if (flag2 == 1)
        //                        {

        //                            GammaUpSpeed(D_lums[index, 1], lumg, Point_692AX[index], RGBValue[index, 1],255, 0xfff, out int nowRegValueAdj);
        //                            adjust_x++;
        //                            if (adjust_x > 50)
        //                            {
        //                                lum_off *= 1.5;  
        //                                adjust_x = 0;
        //                            }
        //                            if ((nowRegValueAdj > 0) && (nowRegValueAdj <= 0xfff))
        //                            {
        //                                RGBValue[index, 1] = nowRegValueAdj;    
        //                                Value2Array((byte)index, array2Value);
        //                            }
        //                            else
        //                            {
        //                                T18.Enabled = false;
        //                                MessageBox.Show("G 自动调节失败，请手动重试");
        //                            }

        //                        }
        //                        if (flag1 == 1)
        //                        {
        //                            GammaUpSpeed(D_lums[index, 2], lumb, Point_692AX[index], RGBValue[index, 2], 255, 0xfff, out int nowRegValueAdj);
        //                            adjust_y++;
        //                            if (adjust_y > 50)
        //                            {
        //                                y_off += 0.0005;   
        //                                adjust_y = 0;
        //                            }
        //                            if ((nowRegValueAdj > 0) && (nowRegValueAdj <= 0xfff))
        //                            {
        //                                RGBValue[index, 2] = nowRegValueAdj;    
        //                                Value2Array((byte)index, array2Value);
        //                            }
        //                            else
        //                            {
        //                                T18.Enabled = false;
        //                                MessageBox.Show("B 自动调节失败，请手动重试");
        //                            }

        //                        }
        //                        auto_gamma_write(RM330_Gamma_r[26 - index], (UInt16)RGBValue[index, 0], RM330_Gamma_g[26 - index], (UInt16)RGBValue[index, 1], RM330_Gamma_b[26 - index], (UInt16)RGBValue[index, 2]);
        //                        GammaAdjustTimes++;
        //                    }
        //                    break;
        //                case 1:                           
        //                case 2:                         
        //                case 3:
        //                case 4:
        //                case 5:
        //                case 6:
        //                case 7:
        //                case 8:
        //                case 9:
        //                case 10:
        //                case 11:
        //                case 12:
        //                case 13:
        //                case 14:

        //                case 15:
        //                case 16:
        //                case 17:
        //                case 18:
        //                case 19:
        //                case 20:
        //                case 21:
        //                case 22:
        //                case 23:



        //                    {

        //                        {
        //                            if (flag3 == 1)
        //                            {   
        //                                GammaUpSpeed(D_lums[index, 0], lumr, Point_692AX[index], RGBValue[index, 0], Point_692AX[index - 1], RGBValue[index - 1, 0], out int nowRegValueAdj);
        //                                adjust_l++;
        //                                if (adjust_l > 50)
        //                                {
        //                                    x_off += 0.0005;  
        //                                    adjust_l = 0;
        //                                }
        //                                if ((nowRegValueAdj > 0) && (nowRegValueAdj <= RGBValue[index - 1, 0] + 10))
        //                                {
        //                                    RGBValue[index, 0] = nowRegValueAdj;    
        //                                    Value2Array((byte)index, array2Value);  
        //                                }
        //                                else
        //                                {
        //                                    T18.Enabled = false;
        //                                    MessageBox.Show("R 自动调节失败，请手动重试");
        //                                }
        //                            }
        //                            if (flag2 == 1)
        //                            {

        //                                GammaUpSpeed(D_lums[index, 1], lumg, Point_692AX[index], RGBValue[index, 1], Point_692AX[index - 1], RGBValue[index - 1, 1], out int nowRegValueAdj);
        //                                adjust_x++;
        //                                if (adjust_x > 50)
        //                                {   if (lum_off<3)
        //                                    lum_off *= 1.5;  
        //                                    adjust_x = 0;
        //                                }
        //                                if ((nowRegValueAdj > 0) && (nowRegValueAdj <= RGBValue[index - 1, 1] + 10))
        //                                {
        //                                    RGBValue[index, 1] = nowRegValueAdj;    
        //                                    Value2Array((byte)index, array2Value);
        //                                }
        //                                else
        //                                {
        //                                    T18.Enabled = false;
        //                                    MessageBox.Show("G 自动调节失败，请手动重试");
        //                                }

        //                            }

        //                            {
        //                                GammaUpSpeed(D_lums[index, 2], lumb, Point_692AX[index], RGBValue[index, 2], Point_692AX[index - 1], RGBValue[index - 1, 2], out int nowRegValueAdj);
        //                                adjust_y++;
        //                                if (adjust_y > 50)
        //                                {
        //                                    y_off += 0.0005;   
        //                                    adjust_y = 0;
        //                                }
        //                                if ((nowRegValueAdj > 0) && (nowRegValueAdj <= RGBValue[index - 1, 2] + 10))
        //                                {
        //                                    RGBValue[index, 2] = nowRegValueAdj;    
        //                                    Value2Array((byte)index, array2Value);
        //                                }
        //                                else
        //                                {
        //                                    T18.Enabled = false;
        //                                    MessageBox.Show("B 自动调节失败，请手动重试");
        //                                }

        //                            }
        //                            auto_gamma_write(RM330_Gamma_r[26 - index], (UInt16)RGBValue[index, 0], RM330_Gamma_g[26 - index], (UInt16)RGBValue[index, 1], RM330_Gamma_b[26 - index], (UInt16)RGBValue[index, 2]);

        //                            GammaAdjustTimes++;
        //                        }

        //                    }
        //                    break;
        //                default:



        //                    break; 

        //            }

        //        }


        //    }
        //}        
        private int[,] RGBValue = new int[26, 3];
        bool array2Value = false;
        private void Value2Array(byte index, bool valuew2rrayflag)
        {
            if (valuew2rrayflag)
            {
                switch (index)
                {
                    case (0):
                        RGBValue[index, 0] = (int)RM330_R255.Value;
                        RGBValue[index, 1] = (int)RM330_G255.Value;
                        RGBValue[index, 2] = (int)RM330_B255.Value;
                        break;
                    case (1):
                        RGBValue[index, 0] = (int)RM330_R239.Value;
                        RGBValue[index, 1] = (int)RM330_G239.Value;
                        RGBValue[index, 2] = (int)RM330_B239.Value;
                        break;
                    case (2):
                        RGBValue[index, 0] = (int)RM330_R223.Value;
                        RGBValue[index, 1] = (int)RM330_G223.Value;
                        RGBValue[index, 2] = (int)RM330_B223.Value;
                        break;
                    case (3):
                        RGBValue[index, 0] = (int)RM330_R207.Value;
                        RGBValue[index, 1] = (int)RM330_G207.Value;
                        RGBValue[index, 2] = (int)RM330_B207.Value;
                        break;
                    case (4):
                        RGBValue[index, 0] = (int)RM330_R191.Value;
                        RGBValue[index, 1] = (int)RM330_G191.Value;
                        RGBValue[index, 2] = (int)RM330_B191.Value;
                        break;
                    case (5):
                        RGBValue[index, 0] = (int)RM330_R175.Value;
                        RGBValue[index, 1] = (int)RM330_G175.Value;
                        RGBValue[index, 2] = (int)RM330_B175.Value;
                        break;
                    case (6):
                        RGBValue[index, 0] = (int)RM330_R159.Value;
                        RGBValue[index, 1] = (int)RM330_G159.Value;
                        RGBValue[index, 2] = (int)RM330_B159.Value;
                        break;
                    case (7):
                        RGBValue[index, 0] = (int)RM330_R143.Value;
                        RGBValue[index, 1] = (int)RM330_G143.Value;
                        RGBValue[index, 2] = (int)RM330_B143.Value;
                        break;
                    case (8):
                        RGBValue[index, 0] = (int)RM330_R127.Value;
                        RGBValue[index, 1] = (int)RM330_G127.Value;
                        RGBValue[index, 2] = (int)RM330_B127.Value;
                        break;
                    case (9):
                        RGBValue[index, 0] = (int)RM330_R111.Value;
                        RGBValue[index, 1] = (int)RM330_G111.Value;
                        RGBValue[index, 2] = (int)RM330_B111.Value;
                        break;
                    case (10):
                        RGBValue[index, 0] = (int)RM330_R95.Value;
                        RGBValue[index, 1] = (int)RM330_G95.Value;
                        RGBValue[index, 2] = (int)RM330_B95.Value;
                        break;
                    case (11):
                        RGBValue[index, 0] = (int)RM330_R79.Value;
                        RGBValue[index, 1] = (int)RM330_G79.Value;
                        RGBValue[index, 2] = (int)RM330_B79.Value;
                        break;
                    case (12):
                        RGBValue[index, 0] = (int)RM330_R63.Value;
                        RGBValue[index, 1] = (int)RM330_G63.Value;
                        RGBValue[index, 2] = (int)RM330_B63.Value;
                        break;
                    case (13):
                        RGBValue[index, 0] = (int)RM330_R55.Value;
                        RGBValue[index, 1] = (int)RM330_G55.Value;
                        RGBValue[index, 2] = (int)RM330_B55.Value;
                        break;
                    case (14):
                        RGBValue[index, 0] = (int)RM330_R47.Value;
                        RGBValue[index, 1] = (int)RM330_G47.Value;
                        RGBValue[index, 2] = (int)RM330_B47.Value;
                        break;
                    case (15):
                        RGBValue[index, 0] = (int)RM330_R39.Value;
                        RGBValue[index, 1] = (int)RM330_G39.Value;
                        RGBValue[index, 2] = (int)RM330_B39.Value;
                        break;
                    case (16):
                        RGBValue[index, 0] = (int)RM330_R35.Value;
                        RGBValue[index, 1] = (int)RM330_G35.Value;
                        RGBValue[index, 2] = (int)RM330_B35.Value;
                        break;
                    case (17):
                        RGBValue[index, 0] = (int)RM330_R31.Value;
                        RGBValue[index, 1] = (int)RM330_G31.Value;
                        RGBValue[index, 2] = (int)RM330_B31.Value;
                        break;
                    case (18):
                        RGBValue[index, 0] = (int)RM330_R27.Value;
                        RGBValue[index, 1] = (int)RM330_G27.Value;
                        RGBValue[index, 2] = (int)RM330_B27.Value;
                        break;
                    case (19):
                        RGBValue[index, 0] = (int)RM330_R23.Value;
                        RGBValue[index, 1] = (int)RM330_G23.Value;
                        RGBValue[index, 2] = (int)RM330_B23.Value;
                        break;
                    case (20):
                        RGBValue[index, 0] = (int)RM330_R19.Value;
                        RGBValue[index, 1] = (int)RM330_G19.Value;
                        RGBValue[index, 2] = (int)RM330_B19.Value;
                        break;
                    case (21):
                        RGBValue[index, 0] = (int)RM330_R15.Value;
                        RGBValue[index, 1] = (int)RM330_G15.Value;
                        RGBValue[index, 2] = (int)RM330_B15.Value;
                        break;
                    case (22):
                        RGBValue[index, 0] = (int)RM330_R11.Value;
                        RGBValue[index, 1] = (int)RM330_G11.Value;
                        RGBValue[index, 2] = (int)RM330_B11.Value;
                        break;
                    case (23):
                        RGBValue[index, 0] = (int)RM330_R7.Value;
                        RGBValue[index, 1] = (int)RM330_G7.Value;
                        RGBValue[index, 2] = (int)RM330_B7.Value;
                        break;
                    case (24):
                        RGBValue[index, 0] = (int)RM330_R3.Value;
                        RGBValue[index, 1] = (int)RM330_G3.Value;
                        RGBValue[index, 2] = (int)RM330_B3.Value;
                        break;
                    case (25):
                        RGBValue[index, 0] = (int)RM330_R1.Value;
                        RGBValue[index, 1] = (int)RM330_G1.Value;
                        RGBValue[index, 2] = (int)RM330_B1.Value;
                        break;
                    default:
                        break;


                }
            }
            else
            {
                switch (index)
                {
                    case (0):
                        RM330_R255.Value = RGBValue[index, 0];
                        RM330_G255.Value = RGBValue[index, 1];
                        RM330_B255.Value = RGBValue[index, 2];
                        break;
                    case (1):
                        RM330_R239.Value = RGBValue[index, 0];
                        RM330_G239.Value = RGBValue[index, 1];
                        RM330_B239.Value = RGBValue[index, 2];
                        break;
                    case (2):
                        RM330_R223.Value = RGBValue[index, 0];
                        RM330_G223.Value = RGBValue[index, 1];
                        RM330_B223.Value = RGBValue[index, 2];
                        break;
                    case (3):
                        RM330_R207.Value = RGBValue[index, 0];
                        RM330_G207.Value = RGBValue[index, 1];
                        RM330_B207.Value = RGBValue[index, 2];
                        break;
                    case (4):
                        RM330_R191.Value = RGBValue[index, 0];
                        RM330_G191.Value = RGBValue[index, 1];
                        RM330_B191.Value = RGBValue[index, 2];
                        break;
                    case (5):
                        RM330_R175.Value = RGBValue[index, 0];
                        RM330_G175.Value = RGBValue[index, 1];
                        RM330_B175.Value = RGBValue[index, 2];
                        break;
                    case (6):
                        RM330_R159.Value = RGBValue[index, 0];
                        RM330_G159.Value = RGBValue[index, 1];
                        RM330_B159.Value = RGBValue[index, 2];
                        break;
                    case (7):
                        RM330_R143.Value = RGBValue[index, 0];
                        RM330_G143.Value = RGBValue[index, 1];
                        RM330_B143.Value = RGBValue[index, 2];
                        break;
                    case (8):
                        RM330_R127.Value = RGBValue[index, 0];
                        RM330_G127.Value = RGBValue[index, 1];
                        RM330_B127.Value = RGBValue[index, 2];
                        break;
                    case (9):
                        RM330_R111.Value = RGBValue[index, 0];
                        RM330_G111.Value = RGBValue[index, 1];
                        RM330_B111.Value = RGBValue[index, 2];
                        break;
                    case (10):
                        RM330_R95.Value = RGBValue[index, 0];
                        RM330_G95.Value = RGBValue[index, 1];
                        RM330_B95.Value = RGBValue[index, 2];
                        break;
                    case (11):
                        RM330_R79.Value = RGBValue[index, 0];
                        RM330_G79.Value = RGBValue[index, 1];
                        RM330_B79.Value = RGBValue[index, 2];
                        break;
                    case (12):
                        RM330_R63.Value = RGBValue[index, 0];
                        RM330_G63.Value = RGBValue[index, 1];
                        RM330_B63.Value = RGBValue[index, 2];
                        break;
                    case (13):
                        RM330_R55.Value = RGBValue[index, 0];
                        RM330_G55.Value = RGBValue[index, 1];
                        RM330_B55.Value = RGBValue[index, 2];
                        break;
                    case (14):
                        RM330_R47.Value = RGBValue[index, 0];
                        RM330_G47.Value = RGBValue[index, 1];
                        RM330_B47.Value = RGBValue[index, 2];
                        break;
                    case (15):
                        RM330_R39.Value = RGBValue[index, 0];
                        RM330_G39.Value = RGBValue[index, 1];
                        RM330_B39.Value = RGBValue[index, 2];
                        break;
                    case (16):
                        RM330_R35.Value = RGBValue[index, 0];
                        RM330_G35.Value = RGBValue[index, 1];
                        RM330_B35.Value = RGBValue[index, 2];
                        break;
                    case (17):
                        RM330_R31.Value = RGBValue[index, 0];
                        RM330_G31.Value = RGBValue[index, 1];
                        RM330_B31.Value = RGBValue[index, 2];
                        break;
                    case (18):
                        RM330_R27.Value = RGBValue[index, 0];
                        RM330_G27.Value = RGBValue[index, 1];
                        RM330_B27.Value = RGBValue[index, 2];
                        break;
                    case (19):
                        RM330_R23.Value = RGBValue[index, 0];
                        RM330_G23.Value = RGBValue[index, 1];
                        RM330_B23.Value = RGBValue[index, 2];
                        break;
                    case (20):
                        RM330_R19.Value = RGBValue[index, 0];
                        RM330_G19.Value = RGBValue[index, 1];
                        RM330_B19.Value = RGBValue[index, 2];
                        break;
                    case (21):
                        RM330_R15.Value = RGBValue[index, 0];
                        RM330_G15.Value = RGBValue[index, 1];
                        RM330_B15.Value = RGBValue[index, 2];
                        break;
                    case (22):
                        RM330_R11.Value = RGBValue[index, 0];
                        RM330_G11.Value = RGBValue[index, 1];
                        RM330_B11.Value = RGBValue[index, 2];
                        break;
                    case (23):
                        RM330_R7.Value = RGBValue[index, 0];
                        RM330_G7.Value = RGBValue[index, 1];
                        RM330_B7.Value = RGBValue[index, 2];
                        break;
                    case (24):
                        RM330_R3.Value = RGBValue[index, 0];
                        RM330_G3.Value = RGBValue[index, 1];
                        RM330_B3.Value = RGBValue[index, 2];
                        break;
                    case (25):
                        RM330_R1.Value = RGBValue[index, 0];
                        RM330_G1.Value = RGBValue[index, 1];
                        RM330_B1.Value = RGBValue[index, 2];
                        break;
                    default:
                        break;


                }
            }
        }
        //private void btnAutogamma_Click(object sender, EventArgs e)
        //{
        //    int flag = 1;
        //    double Lr_d, Lg_d, Lb_d;
        //    D_lums = new double[27, 3];
        //    D_lum = new double[27];
        //    index = 0;
        //    adjust_Rnum = 0;
        //    adjust_Gnum = 0;
        //    adjust_Bnum = 0;
        //    adjust_l = 0;
        //    adjust_x = 0;
        //    adjust_y = 0;
        //    GammaAdjustTimes = 0;
        //    //Button btnTmp = (Button)sender;

        //    btnCalculate330_Click(btnCalculate330,e);
        //    Thread.Sleep(50);

        //    TimTest.Start();
        //    TimeNow = DateTime.Now; 

        //    D_lum[0] = Convert.ToDouble(txt_330_255.Text);
        //    D_lum[1] = Convert.ToDouble(txt_330_239.Text);
        //    D_lum[2] = Convert.ToDouble(txt_330_223.Text);
        //    D_lum[3] = Convert.ToDouble(txt_330_207.Text);
        //    D_lum[4] = Convert.ToDouble(txt_330_191.Text);
        //    D_lum[5] = Convert.ToDouble(txt_330_175.Text);
        //    D_lum[6] = Convert.ToDouble(txt_330_159.Text);
        //    D_lum[7] = Convert.ToDouble(txt_330_143.Text);
        //    D_lum[8] = Convert.ToDouble(txt_330_127.Text);
        //    D_lum[9] = Convert.ToDouble(txt_330_111.Text);
        //    D_lum[10] = Convert.ToDouble(txt_330_95.Text);
        //    D_lum[11] = Convert.ToDouble(txt_330_79.Text);
        //    D_lum[12] = Convert.ToDouble(txt_330_63.Text);
        //    D_lum[13] = Convert.ToDouble(txt_330_55.Text);
        //    D_lum[14] = Convert.ToDouble(txt_330_47.Text);
        //    D_lum[15] = Convert.ToDouble(txt_330_39.Text);
        //    D_lum[16] = Convert.ToDouble(txt_330_35.Text);
        //    D_lum[17] = Convert.ToDouble(txt_330_31.Text);
        //    D_lum[18] = Convert.ToDouble(txt_330_27.Text);
        //    D_lum[19] = Convert.ToDouble(txt_330_23.Text);
        //    D_lum[20] = Convert.ToDouble(txt_330_19.Text);
        //    D_lum[21] = Convert.ToDouble(txt_330_15.Text);
        //    D_lum[22] = Convert.ToDouble(txt_330_11.Text);
        //    D_lum[23] = Convert.ToDouble(txt_330_7.Text);
        //    D_lum[24] = Convert.ToDouble(txt_330_3.Text);
        //    D_lum[25] = Convert.ToDouble(txt_330_1.Text);

        //    D_x = Convert.ToDouble(txt_330_x.Text);
        //    D_y = Convert.ToDouble(txt_330_y.Text);
        //    Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
        //    Lb_d = 1.0;
        //    Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

        //    for (int i = 0; i < 26; i++)
        //    {
        //        D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
        //        D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
        //        D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
        //    }

        //    for (int i = 0; i < 26; i++)
        //    {
        //        Value2Array((byte)i, true);
        //    }

        //    switch (flag)
        //    {
        //        case 1:
        //            {
        //                lum_off = Convert.ToDouble(txt_330_loff.Text);
        //                x_off = Convert.ToDouble(txt_330_xoff.Text);
        //                y_off = Convert.ToDouble(txt_330_yoff.Text);
        //                OLED_Show_Click(btnwhite, e);
        //                T18.Enabled = true;
        //            }
        //            break;
        //        case 2: break;   

        //        default: break;
        //    }
        //}
        //private void btnStopGamma_Click(object sender, EventArgs e)
        //{
        //    TimTest.Stop();
        //    T18.Enabled = false;
        //    flag1 = 1;
        //    flag2 = 1;
        //    flag3 = 1;
        //    adjust_Rnum = 0;
        //    adjust_Gnum = 0;
        //    adjust_Bnum = 0;
        //    adjust_l = 0;
        //    adjust_x = 0;
        //    adjust_y = 0;
        //}
        private void btnAutogamma2AX_Click(object sender, EventArgs e)
        {

            TimTest.Start();
            TimeNow = DateTime.Now; //开始计时
            btnVerfyGamma.Enabled = true;

            teppoint = 1; //记录是否开始调节下一个绑点      
            frgb = 1;//当前调节RGB标志， 1:r;2:G;3:B;
            fdirr = true;
            fdirg = true;
            fdirb = true;

            frgb = 1;//当前调节RGB标志， 1:r;2:G;3:B;

            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lum = new double[26]; //每阶目标亮度值
            D_lums = new double[26, 3]; //填入亮度拆分后的RGB
            index = 0;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
            flag1 = flag2 = flag3 = 1; GammaAdjustTimes = 0;
            k = 0;
            lum_off = Convert.ToDouble(txt_330_loff.Text);
            x_off = Convert.ToDouble(txt_330_xoff.Text);
            y_off = Convert.ToDouble(txt_330_yoff.Text);
            //--收集各绑点目标亮度值---
            //{ 255, 239, 223, 207, 191, 175, 159, 143, 127, 111, 95, 79, 63, 55, 47, 39, 35, 31, 27, 23, 19, 15, 11, 7, 3, 1 };
            for (int i = 0; i < 26; i++)
            {   //计算各绑点目标亮度值
                D_lum[i] = (Math.Pow((Parttern_RM69330[i] / 255.0), Convert.ToDouble(txt_330_gamma.Text)) * Convert.ToDouble(txt_330_l.Text));
            }

            D_x = Convert.ToDouble(txt_330_x.Text); //目标色坐标
            D_y = Convert.ToDouble(txt_330_y.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < 26; i++)    //保存目标绑点RGB分亮度
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }
            gamma_r = RM330_Gamma_r; gamma_g = RM330_Gamma_g; gamma_b = RM330_Gamma_b;   //指定绑点寄存器地址

            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        //lum_off = Convert.ToDouble(txt2AX_lumoff.Text);
                        //x_off = Convert.ToDouble(txt2AX_Xoff.Text);
                        //y_off = Convert.ToDouble(txt2AX_Yoff.Text);                        
                        {
                            OLED_Show_Click(btnwhite, e);
                        };
                        //显示Gamma调节初始画面，并打开定时器开始调整Gamma
                        Msr_CA310();
                        T18.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }

        //--------------------------------------------------------------------
        private int teppoint = 1; //记录是否开始调节下一个绑点      
        private int frgb = 1;//当前调节RGB标志， 1:r;2:G;3:B;

        int step_tmpr = 32, step_tmpg = 32, step_tmpb = 32;//调节初始步长
        bool fdirr = true;
        bool fdirg = true;
        bool fdirb = true;
        byte nowdirection = 0, lastdirection = 0;
        byte nowdirectionG = 0, lastdirectionG = 0; byte nowdirectionB = 0, lastdirectionB = 0;
        byte GammaAdjN_COUNT = 0; byte GammaAdjN_COUNTG = 0; byte GammaAdjN_COUNTB = 0;
        private void GammaUpSpeed(int frgb, double targetlum, double nowlum, int nowpoint, int nowRegValue, int lastpoint, int lastRegValue, out int nowRegValueAdj)
        {

            if ((nowRegValue > lastRegValue) && (index > 2))
            {
                //nowRegValue = lastRegValue - 1; //之前这里的操作容易引起一个极其隐蔽的随机Bug， 例如：nowRegValue=393，lastRegValue=390，step_tmpr=4
                //此时经过上面的操作后nowRegValue=389，看似正常，但是，再接下来的操作中，某些特殊情况下(计算用的RGB色坐标和实际不符等原因)，nowRegValue会被再次加上（理论上应该是减去）step_tmpr
                //此时，nowRegValue+step_tmpr=389+4，结果，经过一个流程处理后，nowRegValue返回了一个和原来一样值，此时调整的过程就会被卡死~
                nowRegValue = lastRegValue - 1 - step_tmpr;
            }
            if ((teppoint != nowpoint))
            {
                teppoint = nowpoint;
                step_tmpr = Convert.ToInt16(numGammaStep.Value);
                step_tmpg = Convert.ToInt16(numGammaStep.Value);
                step_tmpb = Convert.ToInt16(numGammaStep.Value);
                if (nowlum > targetlum) //初始化调节方向
                {
                    if (frgb == 1) fdirr = true; //向前瞄准目标数据
                    else if (frgb == 2) fdirg = true;
                    else if (frgb == 3) fdirb = true;
                }
                else
                {
                    if (frgb == 1) fdirr = false;
                    else if (frgb == 2) fdirg = false;
                    else if (frgb == 3) fdirb = false;
                }
            }
            nowRegValueAdj = nowRegValue;

            if (nowlum > targetlum)
            {
                if (frgb == 1)
                {
                    nowdirection = 1;
                    if (lastdirection != nowdirection)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNT++;
                        if (GammaAdjN_COUNT > extraInfo)
                        {
                            textBox1.Text += "R..";
                            GammaAdjN_COUNT -= 4;
                        }
                    }
                    lastdirection = nowdirection;

                    if (!fdirr)//判断前进方向是否瞄准目标点
                    {
                        if (step_tmpr > 1) step_tmpr >>= 1;
                    }
                    nowRegValueAdj = nowRegValue - step_tmpr;
                    fdirr = true;
                }
                else if (frgb == 2)
                {
                    nowdirectionG = 1;
                    if (lastdirectionG != nowdirectionG)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNTG++;
                        if (GammaAdjN_COUNTG > extraInfo)
                        {
                            textBox1.Text += "G....";
                            GammaAdjN_COUNTG -= 4;
                        }
                    }
                    lastdirectionG = nowdirectionG;

                    if (!fdirg)
                    {
                        if (step_tmpg > 1) step_tmpg >>= 1;

                    }
                    nowRegValueAdj = nowRegValue - step_tmpg;
                    fdirg = true;
                }
                else if (frgb == 3)
                {
                    nowdirectionB = 1;
                    if (lastdirectionB != nowdirectionB)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNTB++;
                        if (GammaAdjN_COUNTB > extraInfo)
                        {
                            textBox1.Text += "B......";
                            GammaAdjN_COUNTB -= 4;
                        }
                    }
                    lastdirectionB = nowdirectionB;

                    if (!fdirr)
                    {
                        if (step_tmpb > 1) step_tmpb >>= 1;
                    }

                    nowRegValueAdj = nowRegValue - step_tmpb;
                    fdirb = true;
                }

            }
            else if (nowlum < targetlum)
            {
                if (frgb == 1)
                {
                    nowdirection = 0;
                    if (lastdirection != nowdirection)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNT++;
                        if (GammaAdjN_COUNT > extraInfo)
                        {
                            textBox1.Text += "R..";
                            GammaAdjN_COUNT = 0;
                        }
                    }
                    lastdirection = nowdirection;

                    if (fdirr)
                    {
                        if ((step_tmpr > 1)) step_tmpr >>= 1;
                    }
                    nowRegValueAdj = nowRegValue + step_tmpr;
                    fdirr = false;
                }
                else if (frgb == 2)
                {
                    nowdirectionG = 0;
                    if (lastdirectionG != nowdirectionG)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNTG++;
                        if (GammaAdjN_COUNTG > extraInfo)
                        {
                            textBox1.Text += "G....";
                            GammaAdjN_COUNTG -= 4;
                        }
                    }
                    lastdirectionG = nowdirectionG;

                    if (fdirg)
                    {
                        if ((step_tmpg > 1)) step_tmpg >>= 1;
                    }
                    nowRegValueAdj = nowRegValue + step_tmpg;
                    fdirg = false;
                }
                else if (frgb == 3)
                {
                    nowdirectionB = 0;
                    if (lastdirectionB != nowdirectionB)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNTB++;
                        if (GammaAdjN_COUNTB > extraInfo)
                        {
                            textBox1.Text += "B......";
                            GammaAdjN_COUNTB -= 4;
                        }
                    }
                    lastdirectionB = nowdirectionB;

                    if (fdirb)
                    {
                        if ((step_tmpb > 1)) step_tmpb >>= 1;
                    }
                    nowRegValueAdj = nowRegValue + step_tmpb;
                    fdirb = false;
                }
            }
            //frgb = 0;
            if (nowRegValueAdj > 0xffe)
            {
                nowRegValueAdj = 0xff0;
                textBox1.Text += "Over 0xFFF.. \r\n";
                textBox1.Select(textBox1.Text.Length, 0);
                textBox1.ScrollToCaret();
            }
            else if (nowRegValueAdj < 1)
            {
                nowRegValueAdj = 10;
                textBox1.Text += "GammaValue <1... \r\n";
                textBox1.Select(textBox1.Text.Length, 0);
                textBox1.ScrollToCaret();
            }

        }
        //------------------------------------------------------------
        //byte k = 0;
        bool array2value = false;   //记录值写入窗体控件(记录绑点RGB寄存器值)的标志位
        bool value2array = true;    //窗体控件值保存到记录标志位
        byte extraInfo; bool gammasetjumpflag = false;//jump 按钮
        private void T18_Tick(object sender, EventArgs e)   //----Gamma 调整----
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            T18.Enabled = false;
            Msr_CA310();                         //获取当前亮度值并确定其是否在规格范围内

            if ((index > 1) && (index <= 18))    //设定重试次数
            {
                extraInfo = Convert.ToByte(nudGammaAdjTimes.Value * 3);
            }
            else if (((index > 18) && (index < 26)))
            {
                extraInfo = Convert.ToByte(nudGammaAdjTimes.Value * 2);
            }
            else { extraInfo = Convert.ToByte(nudGammaAdjTimes.Value * 5); }

            if ((index <= 10))    //设定色坐标误差范围， index=0: 255灰阶
            {
                lum_off = Convert.ToDouble(txt_330_loff.Text);
                x_off = 1.5 * Convert.ToDouble(txt_330_xoff.Text);
                y_off = 1.5 * Convert.ToDouble(txt_330_yoff.Text);
            }
            else if (((index > 18)))
            {
                lum_off = 0.25 * Convert.ToDouble(txt_330_loff.Text);
                x_off = 2.5 * Convert.ToDouble(txt_330_xoff.Text);
                y_off = 2.5 * Convert.ToDouble(txt_330_yoff.Text);
            }
            else
            {
                lum_off = 0.5 * Convert.ToDouble(txt_330_loff.Text);
                x_off = 2 * Convert.ToDouble(txt_330_xoff.Text);
                y_off = 2 * Convert.ToDouble(txt_330_yoff.Text);
            }


            if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//亮度
                flag1 = 0;
            if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//y
                flag2 = 0;
            if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//x
                flag3 = 0;



            if ((flag1 == 0 && flag2 == 0 && flag3 == 0) | (gammasetjumpflag) | ((GammaAdjN_COUNT > extraInfo - 3) && (GammaAdjN_COUNTG > extraInfo - 3) && (GammaAdjN_COUNTB > extraInfo - 3)))//增加绑点跳过gammasetjumpflag
            {
                if (((GammaAdjN_COUNT > extraInfo - 3) && (GammaAdjN_COUNTG > extraInfo - 3) && (GammaAdjN_COUNTB > extraInfo - 3)))
                {
                    textBox1.Text += index + "绑点被模糊处理\r\n";
                }
                GammaAdjN_COUNT = 0;
                gammasetjumpflag = false;
                textBox1.Text += "\r\n" + "绑点" + (index + 1) + "调试OK\r\n";
                //把光标（文本起始点）移动到文件框中，文本的末尾处
                this.textBox1.SelectionStart = this.textBox1.TextLength;
                //然后滚动到光标所在的位置
                this.textBox1.ScrollToCaret();

                index++;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                GammaAdjustTimes = 0;
                if ((index == 26))    //只调25个灰阶，最后一个不处理
                {
                    textBox1.Text = textBox1.Text + "RM69330 gamma调试完毕!\r\n";
                    TimTest.Stop();//停止计时

                    textBox1.Select(textBox1.Text.Length, 0);
                    textBox1.ScrollToCaret();
                    T18.Stop(); return;
                }
                if (index <= 26)
                {
                    {
                        if (cbx_Nor5_AOD.Checked == true)
                        {
                            Send_buf[1] = 0x11;
                            Send_buf[3] = (byte)(MainForm.XX0.Value / 256);
                            Send_buf[4] = (byte)(MainForm.XX0.Value % 256);
                            Send_buf[5] = (byte)(MainForm.XX1.Value / 256);
                            Send_buf[6] = (byte)(MainForm.XX1.Value % 256);
                            Send_buf[7] = (byte)(MainForm.YY0.Value / 256);
                            Send_buf[8] = (byte)(MainForm.YY0.Value % 256);
                            Send_buf[9] = (byte)(MainForm.YY1.Value / 256);
                            Send_buf[10] = (byte)(MainForm.YY1.Value % 256);
                            MainForm.LCD_Cfg_Click(MainForm.btnLCD_Set_Disp_Window_cfg, e);
                            Thread.Sleep(50); //1秒=1000毫秒，
                        }
                        else
                        {
                            //Send_buf[1] = 0x11;   //Autogamma后造成HACTvalue丢失，从而造成显示彩图会异常卡死，待益明后续更新
                            //Send_buf[3] = 0 / 256;
                            //Send_buf[4] = 0 % 256;
                            //Send_buf[5] = (byte)(MainForm.HACT.Value / 256);
                            //Send_buf[6] = (byte)(MainForm.HACT.Value % 256);
                            //Send_buf[7] = 0 / 256;
                            //Send_buf[8] = 0 % 256;
                            //Send_buf[9] = (byte)(MainForm.VACT.Value / 256);
                            //Send_buf[10] = (byte)(MainForm.VACT.Value % 256);
                            //MainForm.LCD_Cfg_Click(MainForm.btnLCD_Set_Disp_Window_cfg, e);
                            //Thread.Sleep(200); //1秒=1000毫秒，
                        }

                        numPatternR.Value = Point_692AX[index];
                        numPatternG.Value = Point_692AX[index];
                        numPatternB.Value = Point_692AX[index];
                        //MainForm.numPatternR.Value = Point_692AX[index];
                        //MainForm.numPatternG.Value = Point_692AX[index];
                        //MainForm.numPatternB.Value = Point_692AX[index];
                        //MainForm.LCD_Cfg_Click(MainForm.btnPatternW, e);
                        OLED_Show_Click(btnPatternW, e);
                        Thread.Sleep(200); //1秒=1000毫秒，
                        //OLED_Show_Click(btnPatternW, e);//写入Pattern参数
                        labtargetlum.Text = "TL： " + D_lum[index];
                    }
                    Thread.Sleep(10); //1秒=1000毫秒，
                    T18.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {   //如果不在规格内则继续调整
                decimal r = 0, g = 0, b = 0;

                Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                Lb_t = 1.0;
                Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

                #region//----收集Gamma RGB 寄存器信息---
                for (int i = 0; i < 26; i++)
                {
                    RGBValueSet(value2array, i, 0, 0);
                    RGBValueSet(value2array, i, 1, 0);
                    RGBValueSet(value2array, i, 2, 0);
                }
                #endregion
                switch (index)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                    case 10:
                    case 11:
                    case 12:
                    case 13:
                    case 14:
                    case 15:
                    case 16:
                    case 17:
                    case 18:
                    case 19:
                    case 20:
                    case 21:
                    case 22:
                    case 23:
                    case 24:
                    case 25:

                        {
                            //if (flag3 == 1)
                            {   //private void GammaUpSpeed(double targetlum, double nowlum, int nowpoint, int nowRegValue,  int lastpoint,int lastRegValue, out int nowRegValueAdj);
                                frgb = 1;
                                if (index > 0)
                                {
                                    GammaUpSpeed(frgb, D_lums[index, 0], lumr, Point_692AX[index], RGBValue[index, 0], Point_692AX[index - 1], RGBValue[index - 1, 0], out int nowRegValueAdj);
                                    if ((nowRegValueAdj > 0))// && (nowRegValueAdj <= RGBValue[index - 1, 0] + 10))
                                    {
                                        RGBValue[index, 0] = nowRegValueAdj;    //数据更新至数组
                                        RGBValueSet(array2value, index, 0, 0);  //通过数组把数据写回控件 //RM692CX_R29.Value = nowRegValueAdj;
                                    }
                                    else
                                    {
                                        T18.Enabled = false;
                                        MessageBoxTimeoutA((IntPtr)0, "R自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                        if ((nowRegValueAdj > 0))// && (nowRegValueAdj <= RGBValue[index - 1, 0] + 10))
                                        {
                                            RGBValue[index, 0] = nowRegValueAdj;    //数据更新至数组
                                            RGBValueSet(array2value, index, 0, 0);  //通过数组把数据写回控件 //RM692CX_R29.Value = nowRegValueAdj;
                                        }
                                        else
                                        {
                                            T18.Enabled = false;
                                            MessageBoxTimeoutA((IntPtr)0, "R自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                        }
                                    }
                                }
                                else
                                {
                                    GammaUpSpeed(frgb, D_lums[index, 0], lumr, Point_692AX[index], RGBValue[index, 0], Point_692AX[index], RGBValue[index, 0], out int nowRegValueAdj);
                                    if ((nowRegValueAdj > 0))// && (nowRegValueAdj <= RGBValue[index - 1, 0] + 10))
                                    {
                                        RGBValue[index, 0] = nowRegValueAdj;    //数据更新至数组
                                        RGBValueSet(array2value, index, 0, 0);  //通过数组把数据写回控件 //RM692CX_R29.Value = nowRegValueAdj;
                                    }
                                    else
                                    {
                                        T18.Enabled = false;
                                        MessageBoxTimeoutA((IntPtr)0, "R自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                    }
                                }

                            }
                            //if (flag2 == 1)
                            {
                                //private void GammaUpSpeed(double targetlum, double nowlum, int nowpoint, int nowRegValue,  int lastpoint,int lastRegValue, out int nowRegValueAdj);
                                frgb = 2;
                                if (index > 1)
                                {
                                    GammaUpSpeed(frgb, D_lums[index, 1], lumg, Point_692AX[index], RGBValue[index, 1], Point_692AX[index - 1], RGBValue[index - 1, 1], out int nowRegValueAdj);
                                    if ((nowRegValueAdj > 0))//&& (nowRegValueAdj <= RGBValue[index - 1, 1] + 10))
                                    {
                                        RGBValue[index, 1] = nowRegValueAdj;    //数据更新至数组
                                        RGBValueSet(array2value, index, 1, 0);
                                    }
                                    else
                                    {
                                        T18.Enabled = false;
                                        MessageBoxTimeoutA((IntPtr)0, "G自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                        //MessageBox.Show("G 自动调节失败，请手动重试");
                                    }
                                }

                                else
                                {
                                    GammaUpSpeed(frgb, D_lums[index, 1], lumg, Point_692AX[index], RGBValue[index, 1], Point_692AX[index - 0], RGBValue[index - 0, 1], out int nowRegValueAdj);
                                    if ((nowRegValueAdj > 0))//&& (nowRegValueAdj <= RGBValue[index - 1, 1] + 10))
                                    {
                                        RGBValue[index, 1] = nowRegValueAdj;    //数据更新至数组
                                        RGBValueSet(array2value, index, 1, 0);
                                    }
                                    else
                                    {
                                        T18.Enabled = false;
                                        MessageBoxTimeoutA((IntPtr)0, "G自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                        //MessageBox.Show("G 自动调节失败，请手动重试");
                                    }
                                }

                            }
                            //if (flag1 == 1)
                            {
                                frgb = 3;
                                if (index > 1)
                                {
                                    GammaUpSpeed(frgb, D_lums[index, 2], lumb, Point_692AX[index], RGBValue[index, 2], Point_692AX[index - 1], RGBValue[index - 1, 2], out int nowRegValueAdj);
                                    if ((nowRegValueAdj > 0))// && (nowRegValueAdj <= RGBValue[index - 1, 2] + 10))
                                    {
                                        RGBValue[index, 2] = nowRegValueAdj;    //数据更新至数组
                                        RGBValueSet(array2value, index, 2, 0);
                                    }
                                    else
                                    {
                                        T18.Enabled = false;
                                        MessageBoxTimeoutA((IntPtr)0, "B自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                        //MessageBox.Show("B 自动调节失败，请手动重试");
                                    }
                                }

                                else
                                {
                                    GammaUpSpeed(frgb, D_lums[index, 2], lumb, Point_692AX[index], RGBValue[index, 2], Point_692AX[index - 0], RGBValue[index - 0, 2], out int nowRegValueAdj);
                                    if ((nowRegValueAdj > 0))// && (nowRegValueAdj <= RGBValue[index - 1, 2] + 10))
                                    {
                                        RGBValue[index, 2] = nowRegValueAdj;    //数据更新至数组
                                        RGBValueSet(array2value, index, 2, 0);
                                    }
                                    else
                                    {
                                        T18.Enabled = false;
                                        MessageBoxTimeoutA((IntPtr)0, "B自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                        //MessageBox.Show("B 自动调节失败，请手动重试");
                                    }
                                }


                            }
                            //GammaDataRevew(32 - index, (UInt16)RGBValue[index, 0], (UInt16)RGBValue[index, 1], (UInt16)RGBValue[index, 2]);
                            auto_gamma_write(gamma_r[26 - index], (UInt16)RGBValue[index, 0], gamma_g[26 - index], (UInt16)RGBValue[index, 1], gamma_b[26 - index], (UInt16)RGBValue[index, 2]);
                            //GammaAdjustTimes++;
                        }
                        break;
                    default: break;
                }
                T18.Enabled = true;
            }
        }
        [DllImport("user32.dll")]
        public static extern int MessageBoxTimeoutA(IntPtr hWnd, string msg, string Caps, int type, int Id, int time);//引用DLL

        private void btn2AXStop_Click(object sender, EventArgs e)   //--停止Gamma调整--
        {
            TimTest.Stop();//停止计时
            T18.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;

            R_x = 0.6756;//
            R_y = 0.3222;
            G_x = 0.2129;
            G_y = 0.7378;
            B_x = 0.1412;
            B_y = 0.0356;
        }

        private void RGBValueSet(bool inoutflag, int index, byte rgb, byte extraInfo)    //把窗体控件上Gamma绑点寄存器值保存于数组，便于后续处理
        {// 255, 239, 223, 207, 191, 175, 159, 143, 127, 111, 95, 79, 63, 55, 47, 39, 35, 31, 27, 23, 19, 15, 11, 7, 3, 1 
            NumericUpDown[] NUMR = new NumericUpDown[] { RM330_R255, RM330_R239, RM330_R223, RM330_R207, RM330_R191, RM330_R175, RM330_R159, RM330_R143, RM330_R127, RM330_R111, RM330_R95, RM330_R79, RM330_R63, RM330_R55, RM330_R47, RM330_R39, RM330_R35, RM330_R31, RM330_R27, RM330_R23, RM330_R19, RM330_R15, RM330_R11, RM330_R7, RM330_R3, RM330_R1 };
            NumericUpDown[] NUMG = new NumericUpDown[] { RM330_G255, RM330_G239, RM330_G223, RM330_G207, RM330_G191, RM330_G175, RM330_G159, RM330_G143, RM330_G127, RM330_G111, RM330_G95, RM330_G79, RM330_G63, RM330_G55, RM330_G47, RM330_G39, RM330_G35, RM330_G31, RM330_G27, RM330_G23, RM330_G19, RM330_G15, RM330_G11, RM330_G7, RM330_G3, RM330_G1 };
            NumericUpDown[] NUMB = new NumericUpDown[] { RM330_B255, RM330_B239, RM330_B223, RM330_B207, RM330_B191, RM330_B175, RM330_B159, RM330_B143, RM330_B127, RM330_B111, RM330_B95, RM330_B79, RM330_B63, RM330_B55, RM330_B47, RM330_B39, RM330_B35, RM330_B31, RM330_B27, RM330_B23, RM330_B19, RM330_B15, RM330_B11, RM330_B7, RM330_B3, RM330_B1 };
            if (inoutflag)
            {
                switch (rgb)
                {
                    case 0:
                        RGBValue[index, 0] = (int)NUMR[index].Value;
                        break;
                    case 1:
                        RGBValue[index, 1] = (int)NUMG[index].Value;
                        break;
                    case 2:
                        RGBValue[index, 2] = (int)NUMB[index].Value;
                        break;
                }
            }
            else
            {
                switch (rgb)
                {
                    case 0:
                        NUMR[index].Value = RGBValue[index, 0];
                        break;
                    case 1:
                        NUMG[index].Value = RGBValue[index, 1];
                        break;
                    case 2:
                        NUMB[index].Value = RGBValue[index, 2];
                        break;
                }
            }
        }

        private void mtLoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {

                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();

                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine();
                }
                fs.Close();
                streamReader.Close();

                string[] arrtmp = strtmp.Split(new Char[] { ',' });



                {
                    RM330_R0.Text = arrtmp[1].ToString(); RM330_G0.Text = arrtmp[3].ToString(); RM330_B0.Text = arrtmp[5].ToString();
                    RM330_R1.Text = arrtmp[7].ToString(); RM330_G1.Text = arrtmp[9].ToString(); RM330_B1.Text = arrtmp[11].ToString();
                    RM330_R3.Text = arrtmp[13].ToString(); RM330_G3.Text = arrtmp[15].ToString(); RM330_B3.Text = arrtmp[17].ToString();
                    RM330_R7.Text = arrtmp[19].ToString(); RM330_G7.Text = arrtmp[21].ToString(); RM330_B7.Text = arrtmp[23].ToString();
                    RM330_R11.Text = arrtmp[25].ToString(); RM330_G11.Text = arrtmp[27].ToString(); RM330_B11.Text = arrtmp[29].ToString();
                    RM330_R15.Text = arrtmp[31].ToString(); RM330_G15.Text = arrtmp[33].ToString(); RM330_B15.Text = arrtmp[35].ToString();
                    RM330_R19.Text = arrtmp[37].ToString(); RM330_G19.Text = arrtmp[39].ToString(); RM330_B19.Text = arrtmp[41].ToString();
                    RM330_R23.Text = arrtmp[43].ToString(); RM330_G23.Text = arrtmp[45].ToString(); RM330_B23.Text = arrtmp[47].ToString();
                    RM330_R27.Text = arrtmp[49].ToString(); RM330_G27.Text = arrtmp[51].ToString(); RM330_B27.Text = arrtmp[53].ToString();
                    RM330_R31.Text = arrtmp[55].ToString(); RM330_G31.Text = arrtmp[57].ToString(); RM330_B31.Text = arrtmp[59].ToString();
                    RM330_R35.Text = arrtmp[61].ToString(); RM330_G35.Text = arrtmp[63].ToString(); RM330_B35.Text = arrtmp[65].ToString();
                    RM330_R39.Text = arrtmp[67].ToString(); RM330_G39.Text = arrtmp[69].ToString(); RM330_B39.Text = arrtmp[71].ToString();
                    RM330_R47.Text = arrtmp[73].ToString(); RM330_G47.Text = arrtmp[75].ToString(); RM330_B47.Text = arrtmp[77].ToString();
                    RM330_R55.Text = arrtmp[79].ToString(); RM330_G55.Text = arrtmp[81].ToString(); RM330_B55.Text = arrtmp[83].ToString();
                    RM330_R63.Text = arrtmp[85].ToString(); RM330_G63.Text = arrtmp[87].ToString(); RM330_B63.Text = arrtmp[89].ToString();
                    RM330_R79.Text = arrtmp[91].ToString(); RM330_G79.Text = arrtmp[93].ToString(); RM330_B79.Text = arrtmp[95].ToString();
                    RM330_R95.Text = arrtmp[97].ToString(); RM330_G95.Text = arrtmp[99].ToString(); RM330_B95.Text = arrtmp[101].ToString();
                    RM330_R111.Text = arrtmp[103].ToString(); RM330_G111.Text = arrtmp[105].ToString(); RM330_B111.Text = arrtmp[107].ToString();
                    RM330_R127.Text = arrtmp[109].ToString(); RM330_G127.Text = arrtmp[111].ToString(); RM330_B127.Text = arrtmp[113].ToString();
                    RM330_R143.Text = arrtmp[115].ToString(); RM330_G143.Text = arrtmp[117].ToString(); RM330_B143.Text = arrtmp[119].ToString();
                    RM330_R159.Text = arrtmp[121].ToString(); RM330_G159.Text = arrtmp[123].ToString(); RM330_B159.Text = arrtmp[125].ToString();
                    RM330_R175.Text = arrtmp[127].ToString(); RM330_G175.Text = arrtmp[129].ToString(); RM330_B175.Text = arrtmp[131].ToString();
                    RM330_R191.Text = arrtmp[133].ToString(); RM330_G191.Text = arrtmp[135].ToString(); RM330_B191.Text = arrtmp[137].ToString();
                    RM330_R207.Text = arrtmp[139].ToString(); RM330_G207.Text = arrtmp[141].ToString(); RM330_B207.Text = arrtmp[143].ToString();
                    RM330_R223.Text = arrtmp[145].ToString(); RM330_G223.Text = arrtmp[147].ToString(); RM330_B223.Text = arrtmp[149].ToString();
                    RM330_R239.Text = arrtmp[151].ToString(); RM330_G239.Text = arrtmp[153].ToString(); RM330_B239.Text = arrtmp[155].ToString();
                    RM330_R255.Text = arrtmp[157].ToString(); RM330_G255.Text = arrtmp[159].ToString(); RM330_B255.Text = arrtmp[161].ToString();
                }



            }
        }
        private void mtSaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*";
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                if (sf.FileName == "") return;
                try
                {
                    StreamWriter sw = null;
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);


                    {
                        str = str + "R0," + RM330_R0.Text + "," + "G0," + RM330_G0.Text + "," + "B0," + RM330_B0.Text + "\r\n";
                        str = str + "R1," + RM330_R1.Text + "," + "G1," + RM330_G1.Text + "," + "B1," + RM330_B1.Text + "\r\n";
                        str = str + "R3," + RM330_R3.Text + "," + "G3," + RM330_G3.Text + "," + "B3," + RM330_B3.Text + "\r\n";
                        str = str + "R7," + RM330_R7.Text + "," + "G7," + RM330_G7.Text + "," + "B7," + RM330_B7.Text + "\r\n";
                        str = str + "R11," + RM330_R11.Text + "," + "G11," + RM330_G11.Text + "," + "B11," + RM330_B11.Text + "\r\n";
                        str = str + "R15," + RM330_R15.Text + "," + "G15," + RM330_G15.Text + "," + "B15," + RM330_B15.Text + "\r\n";
                        str = str + "R19," + RM330_R19.Text + "," + "G19," + RM330_G19.Text + "," + "B19," + RM330_B19.Text + "\r\n";
                        str = str + "R23," + RM330_R23.Text + "," + "G23," + RM330_G23.Text + "," + "B23," + RM330_B23.Text + "\r\n";
                        str = str + "R27," + RM330_R27.Text + "," + "G27," + RM330_G27.Text + "," + "B27," + RM330_B27.Text + "\r\n";
                        str = str + "R31," + RM330_R31.Text + "," + "G31," + RM330_G31.Text + "," + "B31," + RM330_B31.Text + "\r\n";
                        str = str + "R35," + RM330_R35.Text + "," + "G35," + RM330_G35.Text + "," + "B35," + RM330_B35.Text + "\r\n";
                        str = str + "R39," + RM330_R39.Text + "," + "G39," + RM330_G39.Text + "," + "B39," + RM330_B39.Text + "\r\n";
                        str = str + "R47," + RM330_R47.Text + "," + "G47," + RM330_G47.Text + "," + "B47," + RM330_B47.Text + "\r\n";
                        str = str + "R55," + RM330_R55.Text + "," + "G55," + RM330_G55.Text + "," + "B55," + RM330_B55.Text + "\r\n";
                        str = str + "R63," + RM330_R63.Text + "," + "G63," + RM330_G63.Text + "," + "B63," + RM330_B63.Text + "\r\n";
                        str = str + "R79," + RM330_R79.Text + "," + "G79," + RM330_G79.Text + "," + "B79," + RM330_B79.Text + "\r\n";
                        str = str + "R95," + RM330_R95.Text + "," + "G95," + RM330_G95.Text + "," + "B95," + RM330_B95.Text + "\r\n";
                        str = str + "R111," + RM330_R111.Text + "," + "G111," + RM330_G111.Text + "," + "B111," + RM330_B111.Text + "\r\n";
                        str = str + "R127," + RM330_R127.Text + "," + "G127," + RM330_G127.Text + "," + "B127," + RM330_B127.Text + "\r\n";
                        str = str + "R143," + RM330_R143.Text + "," + "G143," + RM330_G143.Text + "," + "B143," + RM330_B143.Text + "\r\n";
                        str = str + "R159," + RM330_R159.Text + "," + "G159," + RM330_G159.Text + "," + "B159," + RM330_B159.Text + "\r\n";
                        str = str + "R175," + RM330_R175.Text + "," + "G175," + RM330_G175.Text + "," + "B175," + RM330_B175.Text + "\r\n";
                        str = str + "R191," + RM330_R191.Text + "," + "G191," + RM330_G191.Text + "," + "B191," + RM330_B191.Text + "\r\n";
                        str = str + "R207," + RM330_R207.Text + "," + "G207," + RM330_G207.Text + "," + "B207," + RM330_B207.Text + "\r\n";
                        str = str + "R223," + RM330_R223.Text + "," + "G223," + RM330_G223.Text + "," + "B223," + RM330_B223.Text + "\r\n";
                        str = str + "R239," + RM330_R239.Text + "," + "G239," + RM330_G239.Text + "," + "B239," + RM330_B239.Text + "\r\n";
                        str = str + "R255," + RM330_R255.Text + "," + "G255," + RM330_G255.Text + "," + "B255," + RM330_B255.Text + "\r\n";
                        sw.WriteLine(str);
                    }

                    sw.Dispose();
                    sw.Close();
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        }

        void auto_gamma_write(UInt16 addr, UInt16 datar, UInt16 addg, UInt16 datag, UInt16 addb, UInt16 Datab)
        {
            Send_buf = new byte[17];
            Setting.TempRecord.cmdLen = 17;

            if ((datar < 0x0008) || (datag < 0x0008) || (Datab < 0x0008) || (datar > 0x0ff0) || (datag > 0x0ff0) || (Datab > 0x0ff0))
            {
                T5.Enabled = false;

                flag1 = 1;
                flag2 = 1;
                flag3 = 1;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                textBox1.Text += "注意！写入数据异常，数值超限\r\n";
            }

            Send_buf[0] = 0x32;
            Send_buf[1] = 0xDD;
            Send_buf[2] = 12;
            Send_buf[3] = Convert.ToByte(addr >> 8);
            Send_buf[4] = Convert.ToByte(datar >> 8);
            Send_buf[5] = Convert.ToByte(addr & 0xff);
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(addg >> 8);
            Send_buf[8] = Convert.ToByte(datag >> 8);
            Send_buf[9] = Convert.ToByte(addg & 0xff);
            Send_buf[10] = Convert.ToByte(datag & 0xff);
            Send_buf[11] = Convert.ToByte(addb >> 8);
            Send_buf[12] = Convert.ToByte(Datab >> 8);
            Send_buf[13] = Convert.ToByte(addb & 0xff);
            Send_buf[14] = Convert.ToByte(Datab & 0xff);
            CRCData.CalculateCrc16(Send_buf, out Send_buf[15], out Send_buf[16]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 17);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
        }
        private void btnCalculate330_Click(object sender, EventArgs e)
        {
            UInt16 i;
            txtTargetLum.Text = "";
            for (i = 0; i < 26; i++)
            {
                txtTargetLum.Text += (i + 1) + "--" + Parttern_RM69330[i] + ": " + (Math.Pow((Parttern_RM69330[i] / 255.0), Convert.ToDouble(txt_330_gamma.Text)) * Convert.ToDouble(txt_330_l.Text)).ToString("F4") + "\r\n";
            }

        }
        public static bool Delay(int delayTime)
        {
            DateTime now = DateTime.Now;
            int s;
            do
            {
                TimeSpan spand = DateTime.Now - now;
                s = spand.Seconds;
                Application.DoEvents();
            }
            while (s < delayTime);
            return true;
        }
        private void RM330_OTP_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x32;
            Send_buf[2] = 0x02;

            switch (btnTmp.Name)
            {
                case "RM69330_gammaset1":
                    {
                        RM330_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0A;
                        Setting.TempRecord.RM69330CmdType = 0x02;
                        Send_buf[3] = 0x00;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try { MainForm.comPort.Write(Send_buf, 0, 7); }
                        catch { MessageBox.Show("串口有误，请检查", "错误"); }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        Delay(100);

                        if (radioButton26.Checked == true)
                        {
                            RM330_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox9.Text) > 2)
                            {
                                MessageBox.Show("OTP次数不能大于2次", "警告");
                                return;
                            }
                            else
                            {
                                RM330_0XFE_XX = 0x00;
                                Send_buf[1] = 0x0A;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try { MainForm.comPort.Write(Send_buf, 0, 7); }
                                catch { MessageBox.Show("串口有误，请检查", "错误"); }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }

                    }
                    break;
                case "RM69330_gammaset2":
                    {
                        RM330_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0D;
                        Setting.TempRecord.RM69330CmdType = 0x02;
                        Send_buf[3] = 0x01;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try { MainForm.comPort.Write(Send_buf, 0, 7); }
                        catch { MessageBox.Show("串口有误，请检查", "错误"); }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        Delay(100);

                        if (radioButton25.Checked == true)
                        {
                            RM330_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox9.Text) > 2)
                            {
                                MessageBox.Show("OTP次数不能大于2次", "警告");
                                return;
                            }
                            else
                            {
                                RM330_0XFE_XX = 0x00;
                                Send_buf[1] = 0x0D;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try { MainForm.comPort.Write(Send_buf, 0, 7); }
                                catch { MessageBox.Show("串口有误，请检查", "错误"); }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM69330_gammaset3":
                    {
                        RM330_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0E;
                        Setting.TempRecord.RM69330CmdType = 0x02;
                        Send_buf[3] = 0x02;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try { MainForm.comPort.Write(Send_buf, 0, 7); }
                        catch { MessageBox.Show("串口有误，请检查", "错误"); }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        Delay(100);

                        if (radioButton24.Checked == true)
                        {
                            RM330_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox9.Text) > 2)
                            {
                                MessageBox.Show("OTP次数不能大于2次", "警告");
                                return;
                            }
                            else
                            {
                                RM330_0XFE_XX = 0x00;
                                Send_buf[1] = 0x0E;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try { MainForm.comPort.Write(Send_buf, 0, 7); }
                                catch { MessageBox.Show("串口有误，请检查", "错误"); }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM330_OTP_GOA":
                    {
                        RM330_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0F;
                        Setting.TempRecord.RM69330CmdType = 0x02;
                        Send_buf[3] = 0x03;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try { MainForm.comPort.Write(Send_buf, 0, 7); }
                        catch { MessageBox.Show("串口有误，请检查", "错误"); }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        Delay(100);

                        if (radioButton23.Checked == true)
                        {
                            RM330_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox9.Text) > 2)
                            {
                                MessageBox.Show("OTP次数不能大于2次", "警告");
                                return;
                            }
                            else
                            {
                                RM330_0XFE_XX = 0x00;
                                Send_buf[1] = 0x0F;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try { MainForm.comPort.Write(Send_buf, 0, 7); }
                                catch { MessageBox.Show("串口有误，请检查", "错误"); }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM330_OTP_Power":
                    {
                        RM330_0XFE_XX = 0x00;
                        Send_buf[1] = 0x10;
                        Setting.TempRecord.RM69330CmdType = 0x02;
                        Send_buf[3] = 0x04;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        try { MainForm.comPort.Write(Send_buf, 0, 7); }
                        catch { MessageBox.Show("串口有误，请检查", "错误"); }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        Delay(100);

                        if (radioButton19.Checked == true)
                        {
                            RM330_0XFE_XX = 0x00;
                            if (Convert.ToDecimal(textBox9.Text) > 2)
                            {
                                MessageBox.Show("OTP次数不能大于2次", "警告");
                                return;
                            }
                            else
                            {
                                RM330_0XFE_XX = 0x00;
                                Send_buf[1] = 0x10;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                                try { MainForm.comPort.Write(Send_buf, 0, 7); }
                                catch { MessageBox.Show("串口有误，请检查", "错误"); }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;

            }
        }

        public ushort[] RM330_Gamma_r = new ushort[27]
       {
            0x0001,0x0203,0x0405,0x0607,0x0809,0x0A0B,0x0C0D,0x0E0F,0x1011,
            0x1213,0x1415,0x1617,0x1819,0x1A1B,0x1C1D,0x1E1F,0x2021,0x2223,
            0x2425,0x2627,0x2829,0x2A2B,0x2D2F,0x3031,0x3233,0x3435,0x3637
       };
        public ushort[] RM330_Gamma_g = new ushort[27]
        {
            0x3839,0x3A3B,0x3D3F,0x4041,0x4243,0x4445,0x4647,0x4849,0x4A4B,
            0x4C4D,0x4E4F,0x5051,0x5253,0x5455,0x5658,0x595A,0x5B5C,0x5D5E,
            0x5F60,0x6162,0x6364,0x6566,0x6768,0x696A,0x6B6C,0x6D6E,0x6F70
        };
        public ushort[] RM330_Gamma_b = new ushort[27]
        {
            0x7172,0x7374,0x7576,0x7778,0x797A,0x7B7C,0x7D7E,0x7F80,0x8182,
            0x8384,0x8586,0x8788,0x898A,0x8B8C,0x8D8E,0x8F90,0x9192,0x9394,
            0x9596,0x9798,0x999A,0x9B9C,0x9D9E,0x9FA0,0xA2A3,0xA4A5,0xA6A7
        };
        public ushort[] RM330_Gamma_r1 = new ushort[13]
         {  0x0001,0x0203,0x0405,0x0607,0x0809,0x0A0B,0x1213,  0x1C1D,0x2021,0x2425,0x2829,0x2D2F,0x3637  };
        public ushort[] RM330_Gamma_g1 = new ushort[13]
         {  0x3839,0x3A3B,0x3D3F,0x4041,0x4243,0x4445,0x4C4D,0x5658,0x5B5C,0x5F60,0x6364,0x6768,0x6F70   };
        public ushort[] RM330_Gamma_b1 = new ushort[13]
         {  0x7172,0x7374,0x7576,0x7778,0x797A,0x7B7C,0x8384,0x8D8E,0x9192,0x9596,0x999A,0x9D9E,0xA6A7 };
        private void RM330_Gamma_Read_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;

            if (radio_RM330_27BD.Checked == true)
            {
                Send_buf = new byte[60];
                Send_buf[0] = 0x32;
                Setting.TempRecord.cmdLen = 60;
                Send_buf[2] = 55;
            }
            else if (radio_RM330_13BD.Checked == true)
            {
                Send_buf = new byte[32];
                Send_buf[0] = 0x32;
                Setting.TempRecord.cmdLen = 32;
                Send_buf[2] = 27;
            }
            Send_buf[3] = 0;

            switch (btnTmp.Name)
            {
                case "RM69330_GammaR_Red":
                    {
                        Setting.TempRecord.RM69330CmdType = 0x03;
                        Send_buf[1] = 0x09;
                        Send_buf[3] = 0x00;
                        if (radio_rm69330_Gamma1.Checked == true) { Send_buf[4] = 0x00; }
                        else if (radio_rm69330_Gamma2.Checked == true) { Send_buf[4] = 0x01; }
                        else if (radio_rm69330_Gamma3.Checked == true) { Send_buf[4] = 0x02; }
                    }
                    break;
                case "RM69330_GammaR_Green":
                    {
                        Setting.TempRecord.RM69330CmdType = 0x04;
                        Send_buf[1] = 0x09;
                        Send_buf[3] = 0x42;
                        if (radio_rm69330_Gamma1.Checked == true) { Send_buf[4] = 0x00; }
                        else if (radio_rm69330_Gamma2.Checked == true) { Send_buf[4] = 0x01; }
                        else if (radio_rm69330_Gamma3.Checked == true) { Send_buf[4] = 0x02; }
                    }
                    break;
                case "RM69330_GammaR_Blue":
                    {
                        Setting.TempRecord.RM69330CmdType = 0x05;
                        Send_buf[1] = 0x09;
                        Send_buf[3] = 0x81;
                        if (radio_rm69330_Gamma1.Checked == true) { Send_buf[4] = 0x00; }
                        else if (radio_rm69330_Gamma2.Checked == true) { Send_buf[4] = 0x01; }
                        else if (radio_rm69330_Gamma3.Checked == true) { Send_buf[4] = 0x02; }
                    }
                    break;

                default:
                    break;
            }
            if (radio_RM330_27BD.Checked == true)
            {
                CRCData.CalculateCrc16(Send_buf, out Send_buf[58], out Send_buf[59]);
                try
                {
                    RM330_0XFE_XX = 0x00;
                    MainForm.comPort.Write(Send_buf, 0, 60);
                }
                catch { MessageBox.Show("串口有误，请检查", "错误"); }
            }
            else if (radio_RM330_13BD.Checked == true)
            {
                CRCData.CalculateCrc16(Send_buf, out Send_buf[30], out Send_buf[31]);
                try
                {
                    RM330_0XFE_XX = 0x00;
                    MainForm.comPort.Write(Send_buf, 0, 32);
                }
                catch { MessageBox.Show("串口有误，请检查", "错误"); }
            }

            /*      try
                   {
                       RM330_0XFE_XX = 0x00;
                       MainForm.comPort.Write(Send_buf, 0, 60);   
                   }
                   catch { MessageBox.Show("串口有误，请检查", "错误"); }

                   */
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }

        private void RM330_Gamma_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;


            if (radio_RM330_27BD.Checked == true)
            {
                Send_buf = new byte[60];
                Send_buf[0] = 0x32;
                Setting.TempRecord.cmdLen = 60;
                Send_buf[2] = 55;
            }
            else if (radio_RM330_13BD.Checked == true)
            {
                Send_buf = new byte[32];
                Send_buf[0] = 0x32;
                Setting.TempRecord.cmdLen = 32;
                Send_buf[2] = 27;
            }
            Send_buf[3] = 0;

            switch (btnTmp.Name)
            {

                case "RM69330_GammaW_Red":
                    {
                        Send_buf[1] = 0x08;
                        if (radio_rm69330_Gamma1.Checked == true) { Send_buf[3] = 0x00; }
                        else if (radio_rm69330_Gamma2.Checked == true) { Send_buf[3] = 0x01; }
                        else if (radio_rm69330_Gamma3.Checked == true) { Send_buf[3] = 0x02; }

                        if ((radio_rm69330_Gamma1.Checked == true) | (radio_rm69330_Gamma2.Checked == true))
                        {
                            if (radio_RM330_27BD.Checked == true)
                            {

                                Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM330_R0.Value.ToString()) >> 8);
                                Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM330_R0.Value.ToString())) & 0xff);
                                Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM330_R1.Value.ToString()) >> 8);
                                Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM330_R1.Value.ToString())) & 0xff);
                                Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM330_R3.Value.ToString()) >> 8);
                                Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM330_R3.Value.ToString())) & 0xff);
                                Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM330_R7.Value.ToString()) >> 8);
                                Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM330_R7.Value.ToString())) & 0xff);
                                Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM330_R11.Value.ToString()) >> 8);
                                Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM330_R11.Value.ToString())) & 0xff);
                                Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM330_R15.Value.ToString()) >> 8);
                                Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM330_R15.Value.ToString())) & 0xff);
                                Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM330_R19.Value.ToString()) >> 8);
                                Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM330_R19.Value.ToString())) & 0xff);
                                Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM330_R23.Value.ToString()) >> 8);
                                Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM330_R23.Value.ToString())) & 0xff);
                                Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM330_R27.Value.ToString()) >> 8);
                                Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM330_R27.Value.ToString())) & 0xff);
                                Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM330_R31.Value.ToString()) >> 8);
                                Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM330_R31.Value.ToString())) & 0xff);
                                Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM330_R35.Value.ToString()) >> 8);
                                Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM330_R35.Value.ToString())) & 0xff);
                                Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM330_R39.Value.ToString()) >> 8);
                                Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM330_R39.Value.ToString())) & 0xff);
                                Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM330_R47.Value.ToString()) >> 8);
                                Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM330_R47.Value.ToString())) & 0xff);
                                Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM330_R55.Value.ToString()) >> 8);
                                Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM330_R55.Value.ToString())) & 0xff);
                                Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM330_R63.Value.ToString()) >> 8);
                                Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM330_R63.Value.ToString())) & 0xff);
                                Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM330_R79.Value.ToString()) >> 8);
                                Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM330_R79.Value.ToString())) & 0xff);
                                Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM330_R95.Value.ToString()) >> 8);
                                Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM330_R95.Value.ToString())) & 0xff);
                                Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM330_R111.Value.ToString()) >> 8);
                                Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM330_R111.Value.ToString())) & 0xff);
                                Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM330_R127.Value.ToString()) >> 8);
                                Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM330_R127.Value.ToString())) & 0xff);
                                Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM330_R143.Value.ToString()) >> 8);
                                Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM330_R143.Value.ToString())) & 0xff);
                                Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM330_R159.Value.ToString()) >> 8);
                                Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM330_R159.Value.ToString())) & 0xff);
                                Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM330_R175.Value.ToString()) >> 8);
                                Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM330_R175.Value.ToString())) & 0xff);
                                Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM330_R191.Value.ToString()) >> 8);
                                Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM330_R191.Value.ToString())) & 0xff);
                                Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM330_R207.Value.ToString()) >> 8);
                                Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM330_R207.Value.ToString())) & 0xff);
                                Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM330_R223.Value.ToString()) >> 8);
                                Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM330_R223.Value.ToString())) & 0xff);
                                Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM330_R239.Value.ToString()) >> 8);
                                Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM330_R239.Value.ToString())) & 0xff);
                                Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM330_R255.Value.ToString()) >> 8);
                                Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM330_R255.Value.ToString())) & 0xff);
                            }
                            else if (radio_RM330_13BD.Checked == true)
                            {
                                Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM330_R0.Value.ToString()) >> 8);
                                Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM330_R0.Value.ToString())) & 0xff);
                                Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM330_R1.Value.ToString()) >> 8);
                                Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM330_R1.Value.ToString())) & 0xff);
                                Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM330_R3.Value.ToString()) >> 8);
                                Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM330_R3.Value.ToString())) & 0xff);
                                Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM330_R7.Value.ToString()) >> 8);
                                Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM330_R7.Value.ToString())) & 0xff);
                                Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM330_R11.Value.ToString()) >> 8);
                                Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM330_R11.Value.ToString())) & 0xff);
                                Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM330_R15.Value.ToString()) >> 8);
                                Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM330_R15.Value.ToString())) & 0xff);
                                Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM330_R31.Value.ToString()) >> 8);
                                Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM330_R31.Value.ToString())) & 0xff);
                                Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM330_R63.Value.ToString()) >> 8);
                                Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM330_R63.Value.ToString())) & 0xff);
                                Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM330_R95.Value.ToString()) >> 8);
                                Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM330_R95.Value.ToString())) & 0xff);
                                Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM330_R127.Value.ToString()) >> 8);
                                Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM330_R127.Value.ToString())) & 0xff);
                                Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM330_R159.Value.ToString()) >> 8);
                                Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM330_R159.Value.ToString())) & 0xff);
                                Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM330_R191.Value.ToString()) >> 8);
                                Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM330_R191.Value.ToString())) & 0xff);
                                Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM330_R255.Value.ToString()) >> 8);
                                Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM330_R255.Value.ToString())) & 0xff);
                            }
                        }
                        else if (radio_rm69330_Gamma3.Checked == true)
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM330_R0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM330_R0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM330_R1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM330_R1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM330_R3.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM330_R3.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM330_R7.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM330_R7.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM330_R11.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM330_R11.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM330_R15.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM330_R15.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM330_R31.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM330_R31.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM330_R63.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM330_R63.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM330_R95.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM330_R95.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM330_R127.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM330_R127.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM330_R159.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM330_R159.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM330_R191.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM330_R191.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM330_R255.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM330_R255.Value.ToString())) & 0xff);
                        }
                    }
                    break;
                case "RM69330_GammaW_Green":
                    {
                        Send_buf[1] = 0x08;
                        if (radio_rm69330_Gamma1.Checked == true) { Send_buf[3] = 0x40; }
                        else if (radio_rm69330_Gamma2.Checked == true) { Send_buf[3] = 0x41; }
                        else if (radio_rm69330_Gamma3.Checked == true) { Send_buf[3] = 0x42; }
                        if ((radio_rm69330_Gamma1.Checked == true) | (radio_rm69330_Gamma2.Checked == true))
                        {
                            if (radio_RM330_27BD.Checked == true)
                            {
                                Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM330_G0.Value.ToString()) >> 8);
                                Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM330_G0.Value.ToString())) & 0xff);
                                Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM330_G1.Value.ToString()) >> 8);
                                Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM330_G1.Value.ToString())) & 0xff);
                                Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM330_G3.Value.ToString()) >> 8);
                                Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM330_G3.Value.ToString())) & 0xff);
                                Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM330_G7.Value.ToString()) >> 8);
                                Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM330_G7.Value.ToString())) & 0xff);
                                Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM330_G11.Value.ToString()) >> 8);
                                Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM330_G11.Value.ToString())) & 0xff);
                                Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM330_G15.Value.ToString()) >> 8);
                                Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM330_G15.Value.ToString())) & 0xff);
                                Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM330_G19.Value.ToString()) >> 8);
                                Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM330_G19.Value.ToString())) & 0xff);
                                Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM330_G23.Value.ToString()) >> 8);
                                Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM330_G23.Value.ToString())) & 0xff);
                                Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM330_G27.Value.ToString()) >> 8);
                                Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM330_G27.Value.ToString())) & 0xff);
                                Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM330_G31.Value.ToString()) >> 8);
                                Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM330_G31.Value.ToString())) & 0xff);
                                Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM330_G35.Value.ToString()) >> 8);
                                Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM330_G35.Value.ToString())) & 0xff);
                                Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM330_G39.Value.ToString()) >> 8);
                                Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM330_G39.Value.ToString())) & 0xff);
                                Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM330_G47.Value.ToString()) >> 8);
                                Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM330_G47.Value.ToString())) & 0xff);
                                Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM330_G55.Value.ToString()) >> 8);
                                Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM330_G55.Value.ToString())) & 0xff);
                                Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM330_G63.Value.ToString()) >> 8);
                                Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM330_G63.Value.ToString())) & 0xff);
                                Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM330_G79.Value.ToString()) >> 8);
                                Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM330_G79.Value.ToString())) & 0xff);
                                Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM330_G95.Value.ToString()) >> 8);
                                Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM330_G95.Value.ToString())) & 0xff);
                                Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM330_G111.Value.ToString()) >> 8);
                                Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM330_G111.Value.ToString())) & 0xff);
                                Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM330_G127.Value.ToString()) >> 8);
                                Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM330_G127.Value.ToString())) & 0xff);
                                Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM330_G143.Value.ToString()) >> 8);
                                Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM330_G143.Value.ToString())) & 0xff);
                                Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM330_G159.Value.ToString()) >> 8);
                                Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM330_G159.Value.ToString())) & 0xff);
                                Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM330_G175.Value.ToString()) >> 8);
                                Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM330_G175.Value.ToString())) & 0xff);
                                Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM330_G191.Value.ToString()) >> 8);
                                Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM330_G191.Value.ToString())) & 0xff);
                                Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM330_G207.Value.ToString()) >> 8);
                                Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM330_G207.Value.ToString())) & 0xff);
                                Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM330_G223.Value.ToString()) >> 8);
                                Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM330_G223.Value.ToString())) & 0xff);
                                Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM330_G239.Value.ToString()) >> 8);
                                Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM330_G239.Value.ToString())) & 0xff);
                                Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM330_G255.Value.ToString()) >> 8);
                                Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM330_G255.Value.ToString())) & 0xff);
                            }
                            else if (radio_RM330_13BD.Checked == true)
                            {
                                Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM330_G0.Value.ToString()) >> 8);
                                Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM330_G0.Value.ToString())) & 0xff);
                                Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM330_G1.Value.ToString()) >> 8);
                                Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM330_G1.Value.ToString())) & 0xff);
                                Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM330_G3.Value.ToString()) >> 8);
                                Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM330_G3.Value.ToString())) & 0xff);
                                Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM330_G7.Value.ToString()) >> 8);
                                Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM330_G7.Value.ToString())) & 0xff);
                                Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM330_G11.Value.ToString()) >> 8);
                                Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM330_G11.Value.ToString())) & 0xff);
                                Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM330_G15.Value.ToString()) >> 8);
                                Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM330_G15.Value.ToString())) & 0xff);
                                Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM330_G31.Value.ToString()) >> 8);
                                Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM330_G31.Value.ToString())) & 0xff);
                                Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM330_G63.Value.ToString()) >> 8);
                                Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM330_G63.Value.ToString())) & 0xff);
                                Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM330_G95.Value.ToString()) >> 8);
                                Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM330_G95.Value.ToString())) & 0xff);
                                Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM330_G127.Value.ToString()) >> 8);
                                Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM330_G127.Value.ToString())) & 0xff);
                                Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM330_G159.Value.ToString()) >> 8);
                                Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM330_G159.Value.ToString())) & 0xff);
                                Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM330_G191.Value.ToString()) >> 8);
                                Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM330_G191.Value.ToString())) & 0xff);
                                Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM330_G255.Value.ToString()) >> 8);
                                Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM330_G255.Value.ToString())) & 0xff);
                            }
                        }
                        else if (radio_rm69330_Gamma3.Checked == true)
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM330_G0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM330_G0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM330_G1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM330_G1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM330_G3.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM330_G3.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM330_G7.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM330_G7.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM330_G11.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM330_G11.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM330_G15.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM330_G15.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM330_G31.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM330_G31.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM330_G63.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM330_G63.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM330_G95.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM330_G95.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM330_G127.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM330_G127.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM330_G159.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM330_G159.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM330_G191.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM330_G191.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM330_G255.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM330_G255.Value.ToString())) & 0xff);
                        }

                    }
                    break;
                case "RM69330_GammaW_Blue":
                    {
                        Send_buf[1] = 0x08;
                        if (radio_rm69330_Gamma1.Checked == true) { Send_buf[3] = 0x80; }
                        else if (radio_rm69330_Gamma2.Checked == true) { Send_buf[3] = 0x81; }
                        else if (radio_rm69330_Gamma3.Checked == true) { Send_buf[3] = 0x82; }
                        if ((radio_rm69330_Gamma1.Checked == true) | (radio_rm69330_Gamma2.Checked == true))
                        {
                            if (radio_RM330_27BD.Checked == true)
                            {
                                Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM330_B0.Value.ToString()) >> 8);
                                Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM330_B0.Value.ToString())) & 0xff);
                                Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM330_B1.Value.ToString()) >> 8);
                                Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM330_B1.Value.ToString())) & 0xff);
                                Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM330_B3.Value.ToString()) >> 8);
                                Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM330_B3.Value.ToString())) & 0xff);
                                Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM330_B7.Value.ToString()) >> 8);
                                Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM330_B7.Value.ToString())) & 0xff);
                                Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM330_B11.Value.ToString()) >> 8);
                                Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM330_B11.Value.ToString())) & 0xff);
                                Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM330_B15.Value.ToString()) >> 8);
                                Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM330_B15.Value.ToString())) & 0xff);
                                Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM330_B19.Value.ToString()) >> 8);
                                Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM330_B19.Value.ToString())) & 0xff);
                                Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM330_B23.Value.ToString()) >> 8);
                                Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM330_B23.Value.ToString())) & 0xff);
                                Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM330_B27.Value.ToString()) >> 8);
                                Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM330_B27.Value.ToString())) & 0xff);
                                Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM330_B31.Value.ToString()) >> 8);
                                Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM330_B31.Value.ToString())) & 0xff);
                                Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM330_B35.Value.ToString()) >> 8);
                                Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM330_B35.Value.ToString())) & 0xff);
                                Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM330_B39.Value.ToString()) >> 8);
                                Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM330_B39.Value.ToString())) & 0xff);
                                Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM330_B47.Value.ToString()) >> 8);
                                Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM330_B47.Value.ToString())) & 0xff);
                                Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM330_B55.Value.ToString()) >> 8);
                                Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM330_B55.Value.ToString())) & 0xff);
                                Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM330_B63.Value.ToString()) >> 8);
                                Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM330_B63.Value.ToString())) & 0xff);
                                Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM330_B79.Value.ToString()) >> 8);
                                Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM330_B79.Value.ToString())) & 0xff);
                                Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM330_B95.Value.ToString()) >> 8);
                                Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM330_B95.Value.ToString())) & 0xff);
                                Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM330_B111.Value.ToString()) >> 8);
                                Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM330_B111.Value.ToString())) & 0xff);
                                Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM330_B127.Value.ToString()) >> 8);
                                Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM330_B127.Value.ToString())) & 0xff);
                                Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM330_B143.Value.ToString()) >> 8);
                                Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM330_B143.Value.ToString())) & 0xff);
                                Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM330_B159.Value.ToString()) >> 8);
                                Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM330_B159.Value.ToString())) & 0xff);
                                Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM330_B175.Value.ToString()) >> 8);
                                Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM330_B175.Value.ToString())) & 0xff);
                                Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM330_B191.Value.ToString()) >> 8);
                                Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM330_B191.Value.ToString())) & 0xff);
                                Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM330_B207.Value.ToString()) >> 8);
                                Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM330_B207.Value.ToString())) & 0xff);
                                Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM330_B223.Value.ToString()) >> 8);
                                Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM330_B223.Value.ToString())) & 0xff);
                                Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM330_B239.Value.ToString()) >> 8);
                                Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM330_B239.Value.ToString())) & 0xff);
                                Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM330_B255.Value.ToString()) >> 8);
                                Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM330_B255.Value.ToString())) & 0xff);
                            }
                            else if (radio_RM330_13BD.Checked == true)
                            {
                                Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM330_B0.Value.ToString()) >> 8);
                                Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM330_B0.Value.ToString())) & 0xff);
                                Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM330_B1.Value.ToString()) >> 8);
                                Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM330_B1.Value.ToString())) & 0xff);
                                Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM330_B3.Value.ToString()) >> 8);
                                Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM330_B3.Value.ToString())) & 0xff);
                                Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM330_B7.Value.ToString()) >> 8);
                                Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM330_B7.Value.ToString())) & 0xff);
                                Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM330_B11.Value.ToString()) >> 8);
                                Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM330_B11.Value.ToString())) & 0xff);
                                Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM330_B15.Value.ToString()) >> 8);
                                Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM330_B15.Value.ToString())) & 0xff);
                                Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM330_B31.Value.ToString()) >> 8);
                                Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM330_B31.Value.ToString())) & 0xff);
                                Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM330_B63.Value.ToString()) >> 8);
                                Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM330_B63.Value.ToString())) & 0xff);
                                Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM330_B95.Value.ToString()) >> 8);
                                Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM330_B95.Value.ToString())) & 0xff);
                                Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM330_B127.Value.ToString()) >> 8);
                                Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM330_B127.Value.ToString())) & 0xff);
                                Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM330_B159.Value.ToString()) >> 8);
                                Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM330_B159.Value.ToString())) & 0xff);
                                Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM330_B191.Value.ToString()) >> 8);
                                Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM330_B191.Value.ToString())) & 0xff);
                                Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM330_B255.Value.ToString()) >> 8);
                                Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM330_B255.Value.ToString())) & 0xff);
                            }
                        }
                        else if (radio_rm69330_Gamma3.Checked == true)
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM330_B0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM330_B0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM330_B1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM330_B1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM330_B3.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM330_B3.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM330_B7.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM330_B7.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM330_B11.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM330_B11.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM330_B15.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM330_B15.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM330_B31.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM330_B31.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM330_B63.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM330_B63.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM330_B95.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM330_B95.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM330_B127.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM330_B127.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM330_B159.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM330_B159.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM330_B191.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM330_B191.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM330_B255.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM330_B255.Value.ToString())) & 0xff);
                        }

                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[58], out Send_buf[59]);
            try
            {
                RM330_0XFE_XX = 0x00;
                MainForm.comPort.Write(Send_buf, 0, 60);
            }
            catch { MessageBox.Show("串口有误，请检查", "错误"); }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }

        private void trackBar_330_Scroll(object sender, EventArgs e)
        {
            {
                RM69330_51Reg.Text = RM69330_51Reg_trackBar1.Value.ToString() + "阶";
                if (RM330_0XFE_XX != 0xF0)
                {
                    RM330_0XFE_XX = 0xF0;
                    RM69330_Reg_Addr.Text = "fe";
                    RM69330_Reg_Data.Text = "00";
                    RM330_Others_Click(RM69330_51Reg_W, e);
                }
                RM69330_Reg_Addr.Text = "51";
                RM69330_Reg_Data.Text = RM69330_51Reg_trackBar1.Value.ToString("x2");
                RM330_Others_Click(RM69330_51Reg_W, e);
            }
        }
        private int[] tmp_gamma_330 = new int[81];
        private int adjust_num = 0;
        private string adjust_class = null;

        private void RM69330_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible == true) //子窗体已开启标识
            {
                //MainForm.comPort.Close();
                Thread.Sleep(5);
                MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);

                Thread.Sleep(5);
                MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
                Thread.Sleep(5);
                //MainForm.comPort.Open();
                textBox1.Text += "com 已经连接！\r\n";
            }
            else
            {

            }
        }

        public byte RM330_0XFE_XX = 0;
        private void RM330_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;
            Send_buf[0] = 0x32;

            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "RM69330_Reg_R":
                    {
                        Setting.TempRecord.RM69330CmdType = 0x01;
                        Send_buf[1] = 0x02;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(RM69330_Reg_Addr.Text.Trim(), 16)).ToString());
                        RM330_0XFE_XX = 0x00;
                    }
                    break;

                case "RM69330_51Reg_W":
                    {
                        Send_buf[1] = 0x51;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(RM69330_Reg_Addr.Text.Trim(), 16)).ToString());
                        Send_buf[4] = byte.Parse((Convert.ToInt32(RM69330_Reg_Data.Text.Trim(), 16)).ToString());


                    }
                    break;

                case "RM69330_Reg_W":
                    {
                        Send_buf[1] = 0x01;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(RM69330_Reg_Addr.Text.Trim(), 16)).ToString());
                        Send_buf[4] = byte.Parse((Convert.ToInt32(RM69330_Reg_Data.Text.Trim(), 16)).ToString());
                        if ((Send_buf[3] == 0xfe) && (Send_buf[4] != 0))
                            RM330_0XFE_XX = 0x00;
                    }
                    break;

                case "ReadTime":
                    {
                        RM330_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0B;
                        Setting.TempRecord.RM69330CmdType = 0x02;
                        if (radioButton26.Checked == true) { Send_buf[3] = 0x00; }
                        else if (radioButton25.Checked == true) { Send_buf[3] = 0x01; }
                        else if (radioButton24.Checked == true) { Send_buf[3] = 0x02; }
                        else if (radioButton23.Checked == true) { Send_buf[3] = 0x03; }
                        else if (radioButton19.Checked == true) { Send_buf[3] = 0x04; }
                    }
                    break;
                case "WriteTime":
                    {
                        RM330_0XFE_XX = 0x00;
                        if (Convert.ToDecimal(textBox9.Text) > 2)
                        {
                            MessageBox.Show("OTP次数不能大于2次", "警告");
                            return;
                        }
                        else
                        {
                            Send_buf[1] = 0x0C;
                            Send_buf[4] = byte.Parse((Convert.ToInt32(textBox9.Text.Trim(), 16)).ToString());
                        }
                    }
                    break;

                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try { MainForm.comPort.Write(Send_buf, 0, 7); }
            catch { MessageBox.Show("串口有误，请检查", "错误"); }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }

        private void radio_Gamma4_CheckedChanged(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Setting.TempRecord.cmdLen = 7;
            RadioButton btnTmp = (RadioButton)sender;
            Setting.TempRecord.RM69330CmdType = 0x06;
            Send_buf[0] = 0x32;
            Send_buf[2] = 0x02;
            if (radio_rm69330_Gamma1.Checked == true)
            {
                Send_buf[1] = 0x11;
                Send_buf[3] = 0x01;
                radio_RM330_27BD.Enabled = true;
                if (radio_RM330_27BD.Checked == true)
                {
                    Send_buf[4] = 0x01;
                    RM330_R19.Enabled = true; RM330_G19.Enabled = true; RM330_B19.Enabled = true;
                    RM330_R23.Enabled = true; RM330_G23.Enabled = true; RM330_B23.Enabled = true;
                    RM330_R27.Enabled = true; RM330_G27.Enabled = true; RM330_B27.Enabled = true;
                    RM330_R35.Enabled = true; RM330_G35.Enabled = true; RM330_B35.Enabled = true;
                    RM330_R39.Enabled = true; RM330_G39.Enabled = true; RM330_B39.Enabled = true;
                    RM330_R47.Enabled = true; RM330_G47.Enabled = true; RM330_B47.Enabled = true;
                    RM330_R55.Enabled = true; RM330_G55.Enabled = true; RM330_B55.Enabled = true;
                    RM330_R79.Enabled = true; RM330_G79.Enabled = true; RM330_B79.Enabled = true;
                    RM330_R111.Enabled = true; RM330_G111.Enabled = true; RM330_B111.Enabled = true;
                    RM330_R143.Enabled = true; RM330_G143.Enabled = true; RM330_B143.Enabled = true;
                    RM330_R175.Enabled = true; RM330_G175.Enabled = true; RM330_B175.Enabled = true;
                    RM330_R207.Enabled = true; RM330_G207.Enabled = true; RM330_B207.Enabled = true;
                    RM330_R223.Enabled = true; RM330_G223.Enabled = true; RM330_B223.Enabled = true;
                    RM330_R239.Enabled = true; RM330_G239.Enabled = true; RM330_B239.Enabled = true;
                }
                else if (radio_RM330_13BD.Checked == true)
                {
                    Send_buf[4] = 0x02;
                    RM330_R19.Enabled = false; RM330_G19.Enabled = false; RM330_B19.Enabled = false;
                    RM330_R23.Enabled = false; RM330_G23.Enabled = false; RM330_B23.Enabled = false;
                    RM330_R27.Enabled = false; RM330_G27.Enabled = false; RM330_B27.Enabled = false;
                    RM330_R35.Enabled = false; RM330_G35.Enabled = false; RM330_B35.Enabled = false;
                    RM330_R39.Enabled = false; RM330_G39.Enabled = false; RM330_B39.Enabled = false;
                    RM330_R47.Enabled = false; RM330_G47.Enabled = false; RM330_B47.Enabled = false;
                    RM330_R55.Enabled = false; RM330_G55.Enabled = false; RM330_B55.Enabled = false;
                    RM330_R79.Enabled = false; RM330_G79.Enabled = false; RM330_B79.Enabled = false;
                    RM330_R111.Enabled = false; RM330_G111.Enabled = false; RM330_B111.Enabled = false;
                    RM330_R143.Enabled = false; RM330_G143.Enabled = false; RM330_B143.Enabled = false;
                    RM330_R175.Enabled = false; RM330_G175.Enabled = false; RM330_B175.Enabled = false;
                    RM330_R207.Enabled = false; RM330_G207.Enabled = false; RM330_B207.Enabled = false;
                    RM330_R223.Enabled = false; RM330_G223.Enabled = false; RM330_B223.Enabled = false;
                    RM330_R239.Enabled = false; RM330_G239.Enabled = false; RM330_B239.Enabled = false;
                }


            }
            else if (radio_rm69330_Gamma2.Checked == true)
            {
                Send_buf[1] = 0x11;
                Send_buf[3] = 0x02;
                radio_RM330_27BD.Enabled = true;
            }
            else if (radio_rm69330_Gamma3.Checked == true)
            {
                Send_buf[1] = 0x11;
                Send_buf[3] = 0x03;
                radio_RM330_27BD.Enabled = false;
                radio_RM330_13BD.Checked = true;
                Send_buf[4] = 0x02;
                RM330_R19.Enabled = false; RM330_G19.Enabled = false; RM330_B19.Enabled = false;
                RM330_R23.Enabled = false; RM330_G23.Enabled = false; RM330_B23.Enabled = false;
                RM330_R27.Enabled = false; RM330_G27.Enabled = false; RM330_B27.Enabled = false;
                RM330_R35.Enabled = false; RM330_G35.Enabled = false; RM330_B35.Enabled = false;
                RM330_R39.Enabled = false; RM330_G39.Enabled = false; RM330_B39.Enabled = false;
                RM330_R47.Enabled = false; RM330_G47.Enabled = false; RM330_B47.Enabled = false;
                RM330_R55.Enabled = false; RM330_G55.Enabled = false; RM330_B55.Enabled = false;
                RM330_R79.Enabled = false; RM330_G79.Enabled = false; RM330_B79.Enabled = false;
                RM330_R111.Enabled = false; RM330_G111.Enabled = false; RM330_B111.Enabled = false;
                RM330_R143.Enabled = false; RM330_G143.Enabled = false; RM330_B143.Enabled = false;
                RM330_R175.Enabled = false; RM330_G175.Enabled = false; RM330_B175.Enabled = false;
                RM330_R207.Enabled = false; RM330_G207.Enabled = false; RM330_B207.Enabled = false;
                RM330_R223.Enabled = false; RM330_G223.Enabled = false; RM330_B223.Enabled = false;
                RM330_R239.Enabled = false; RM330_G239.Enabled = false; RM330_B239.Enabled = false;

            }
            if (radio_RM330_27BD.Checked == true)
            {
                Send_buf[4] = 0x01;
                switch (btnTmp.Name)
                {
                    case "radio_RM330_27BD":
                        {






                            RM330_R19.Enabled = true; RM330_G19.Enabled = true; RM330_B19.Enabled = true;
                            RM330_R23.Enabled = true; RM330_G23.Enabled = true; RM330_B23.Enabled = true;
                            RM330_R27.Enabled = true; RM330_G27.Enabled = true; RM330_B27.Enabled = true;
                            RM330_R35.Enabled = true; RM330_G35.Enabled = true; RM330_B35.Enabled = true;
                            RM330_R39.Enabled = true; RM330_G39.Enabled = true; RM330_B39.Enabled = true;
                            RM330_R47.Enabled = true; RM330_G47.Enabled = true; RM330_B47.Enabled = true;
                            RM330_R55.Enabled = true; RM330_G55.Enabled = true; RM330_B55.Enabled = true;
                            RM330_R79.Enabled = true; RM330_G79.Enabled = true; RM330_B79.Enabled = true;
                            RM330_R111.Enabled = true; RM330_G111.Enabled = true; RM330_B111.Enabled = true;
                            RM330_R143.Enabled = true; RM330_G143.Enabled = true; RM330_B143.Enabled = true;
                            RM330_R175.Enabled = true; RM330_G175.Enabled = true; RM330_B175.Enabled = true;
                            RM330_R207.Enabled = true; RM330_G207.Enabled = true; RM330_B207.Enabled = true;
                            RM330_R223.Enabled = true; RM330_G223.Enabled = true; RM330_B223.Enabled = true;
                            RM330_R239.Enabled = true; RM330_G239.Enabled = true; RM330_B239.Enabled = true;

                        }
                        break;
                    case "radio_RM330_13BD":
                        {






                            RM330_R19.Enabled = false; RM330_G19.Enabled = false; RM330_B19.Enabled = false;
                            RM330_R23.Enabled = false; RM330_G23.Enabled = false; RM330_B23.Enabled = false;
                            RM330_R27.Enabled = false; RM330_G27.Enabled = false; RM330_B27.Enabled = false;
                            RM330_R35.Enabled = false; RM330_G35.Enabled = false; RM330_B35.Enabled = false;
                            RM330_R39.Enabled = false; RM330_G39.Enabled = false; RM330_B39.Enabled = false;
                            RM330_R47.Enabled = false; RM330_G47.Enabled = false; RM330_B47.Enabled = false;
                            RM330_R55.Enabled = false; RM330_G55.Enabled = false; RM330_B55.Enabled = false;
                            RM330_R79.Enabled = false; RM330_G79.Enabled = false; RM330_B79.Enabled = false;
                            RM330_R111.Enabled = false; RM330_G111.Enabled = false; RM330_B111.Enabled = false;
                            RM330_R143.Enabled = false; RM330_G143.Enabled = false; RM330_B143.Enabled = false;
                            RM330_R175.Enabled = false; RM330_G175.Enabled = false; RM330_B175.Enabled = false;
                            RM330_R207.Enabled = false; RM330_G207.Enabled = false; RM330_B207.Enabled = false;
                            RM330_R223.Enabled = false; RM330_G223.Enabled = false; RM330_B223.Enabled = false;
                            RM330_R239.Enabled = false; RM330_G239.Enabled = false; RM330_B239.Enabled = false;
                        }
                        break;
                    default: break;

                }
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                RM330_0XFE_XX = 0x00;
                MainForm.comPort.Write(Send_buf, 0, 7);
            }
            catch { MessageBox.Show("串口有误，请检查", "错误"); }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }

    }
}
